Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

c# - ScrapySharp表单提交导致System.AggregateException(ScrapySharp Form Submit causing System.AggregateException)

I spent hours racking my head as to why this isn't working

(我花了几个小时弄清楚为什么这不起作用)

I'm trying to use ScrapySharp to scrape websites, right now just trying out sample sites then moving to my actual site.

(我正在尝试使用ScrapySharp抓取网站,现在只是尝试示例网站,然后再移至我的实际网站。)

Every time I do a form.Submit() in my program I get hit with a System.AggregateException (Specified Cast is Invalid)

(每次我在程序中执行form.Submit() ,都会遇到System.AggregateException form.Submit() (指定的Cast无效))

My code:

(我的代码:)

using System;
using System.IO;
using System.Linq;
using System.Net;
using HtmlAgilityPack;
using ScrapySharp.Extensions;
using ScrapySharp.Html;
using ScrapySharp.Html.Forms;
using ScrapySharp.Network;

namespace WebScraper
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            ScrapingBrowser browser = new ScrapingBrowser();

            //set UseDefaultCookiesParser as false if a website returns invalid cookies format
            //browser.UseDefaultCookiesParser = false;
            browser.AllowAutoRedirect = true;
            browser.AllowMetaRedirect = true;
            WebPage homePage = browser.NavigateToPage(new Uri("http://the-internet.herokuapp.com/login"));

            PageWebForm form = homePage.FindForm("login");
            form["username"] = "tomsmith";
            form["password"] = "SuperSecretPassword!";
            form.Method = HttpVerb.Get; //I tried both .Post and .Get
            WebPage resultsPage = form.Submit(); //THIS IS WHERE I GET THE ERROR
            Console.WriteLine(resultsPage);

        }
    }
}

My error:

(我的错误:)

System.AggregateException: One or more errors occurred.

(System.AggregateException:发生一个或多个错误。)

(Specified cast is not valid.) ---> System.InvalidCastException: Specified cast is not valid.

((指定的转换无效。)---> System.InvalidCastException:指定的转换无效。)

at ScrapySharp.Network.ScrapingBrowser.CreateRequest (System.Uri url, ScrapySharp.Network.HttpVerb verb) [0x0000b] in <0a639adc663f45108f057c429262c620>:0 at ScrapySharp.Network.ScrapingBrowser.NavigateToPageAsync (System.Uri url, ScrapySharp.Network.HttpVerb verb, System.String data, System.String contentType) [0x00066] in <0a639adc663f45108f057c429262c620>:0 --- End of inner exception stack trace --- at System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) [0x00011] in /Users/builder/jenkins/workspace/build-package-osx-mono/2019-06/external/bockbuild/builds/mono-x64/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2027 at System.Threading.Tasks.Task 1[TResult].GetResultCore (System.Boolean waitCompletionNotification) [0x0002b] in /Users/builder/jenkins/workspace/build-package-osx-mono/2019-06/external/bockbuild/builds/mono-x64/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Future.cs:496 at System.Threading.Tasks.Task

(在ScrapySharp.Network.ScrapingBrowser.NavigateToPageAsync(System.UriSharp.Http.ScrapySharp.Network.ScrapingBrowser.NavigateToPageAsync(System.UriSharp.Http。 ,System.String数据,System.String contentType)在<0a639adc663f45108f057c429262c620>:0中的[0x00066]-内部异常堆栈跟踪的结尾-在System.Threading.Tasks.Task.ThrowIfExceptional(System.Boolean includeTaskCanceledExceptions)[0x00011 /Users/builder/jenkins/workspace/build-package-osx-mono/2019-06/external/bockbuild/builds/mono-x64/external/corert/src/System.Private.CoreLib/src/System/Threading中的] /Tasks/Task.cs:2027, 1[TResult].GetResultCore (System.Boolean waitCompletionNotification) [0x0002b] in /Users/builder/jenkins/workspace/build-package-osx-mono/2019-06/external/bockbuild/builds/mono-x64/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Future.cs:496 at System.Threading.Tasks.Task中的System.Threading.Tasks.Task 1[TResult].GetResultCore (System.Boolean waitCompletionNotification) [0x0002b] in /Users/builder/jenkins/workspace/build-package-osx-mono/2019-06/external/bockbuild/builds/mono-x64/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Future.cs:496 at System.Threading.Tasks.Task)

1[TResult].GetResultCore (System.Boolean waitCompletionNotification) [0x0002b] in /Users/builder/jenkins/workspace/build-package-osx-mono/2019-06/external/bockbuild/builds/mono-x64/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Future.cs:496 at System.Threading.Tasks.Task 1[TResult].get_Result () [0x00000] in /Users/builder/jenkins/workspace/build-package-osx-mono/2019-06/external/bockbuild/builds/mono-x64/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Future.cs:466 at ScrapySharp.Network.ScrapingBrowser.NavigateToPage (System.Uri url, ScrapySharp.Network.HttpVerb verb, System.String data, System.String contentType) [0x0000b] in <0a639adc663f45108f057c429262c620>:0 at ScrapySharp.Html.Forms.PageWebForm.Submit () [0x00023] in <0a639adc663f45108f057c429262c620>:0 at WebScraper.MainClass.Main (System.String[] args) [0x00065] in /Users/arib/Projects/WebScraper/WebScraper/Program.cs:29

(/ Users / builder / jenkins / workspace / build-package-osx-mono / 2019-06 / external / bockbuild / builds / mono-x64 /中的1[TResult].GetResultCore (System.Boolean waitCompletionNotification) [0x0002b] in /Users/builder/jenkins/workspace/build-package-osx-mono/2019-06/external/bockbuild/builds/mono-x64/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Future.cs:496 at System.Threading.Tasks.Task 1 [TResult] .get_Result()[0x00000] external / corert / src / System.Private.CoreLib / src / System / Threading / Tasks / Future.cs:466在ScrapySharp.Network.ScrapingBrowser.NavigateToPage(System.Uri url,ScrapySharp.Network.HttpVerb动词,System.String数据,System.String contentType)[0x0000b]在ScrapySharp.Html.Forms.PageWebForm.Submit()的<0a639adc663f45108f057c429262c620>:0中(0 [000x00023]在WebScrapers.MainClass.Main(System.String)的<0a639adc663f45108f057c429262c620>:0中/Users/arib/Projects/WebScraper/WebScraper/Program.cs中的[0x00065]:29)

I'm so tired of this error, any and all help is much appreciated.. Thank you

(我对此错误感到非常厌倦,我们将不胜感激。.谢谢)

  ask by arcanium0611 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...