我试图从网页中提取数据有很多索引
this.webBrowser1.Document.InvokeScript("__doPostBack", new object[] { "ctl00$ContentPlaceHolder1$gvTickets", "Page$2" });
这些代码正在工作并在webbrowser中转到第二页但是在调试时它没有回到documentcompleted方法。
我怎样才能在secodpage上获得响应文本
答案 0 :(得分:1)
这是一个代码示例。 从Console应用程序调用此方法 - 因此线程执行和 如果您使用控制台应用程序,请将[STAThread]属性添加到您的main方法。 documentCompleted将被递归调用,直到条件失败。
protected override void StartScan( string url )
{
WebBrowser browser = new WebBrowser();
int i = 1;
Thread thread = new Thread( () =>
{
WebBrowserDocumentCompletedEventHandler documentCompleted = null;
documentCompleted = async ( o, s ) =>
{
if ( i < 27 )
{
HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
document.LoadHtml(browser.DocumentText);
List<Establishment> establishments = GetEstablishments(document).ToList();
SaveResults(establishments);
string var = i < 10 ? "0" + i.ToString(CultureInfo.InvariantCulture) : i.ToString(CultureInfo.InvariantCulture);
browser.Navigate( string.Format( "javascript:__doPostBack('SchoolDirectoryWebPart1$ctl{0}','')", var ) );
i++;
} else {
browser.Dispose();
Application.ExitThread();
}
};
browser.ScriptErrorsSuppressed = true;
browser.DocumentCompleted += documentCompleted;
browser.Navigate( url );
Application.Run();
} );
thread.SetApartmentState( ApartmentState.STA );
thread.Start();
}