62,266
社区成员
发帖
与我相关
我的任务
分享
static System.Windows.Forms.WebBrowser wb;
private static string url ="http://www.baidu.com";
private static List<MyCount> myCounts = new List<MyCount>();
[STAThread]
protected void Page_Load(object sender, EventArgs e)
{
try
{
System.Threading.Thread t = new System.Threading.Thread(new ThreadStart(() =>
{
wb = new System.Windows.Forms.WebBrowser();
wb.DocumentCompleted += wb_DocumentCompleted;
wb.Navigate(url);
while (wb.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents(); //避免假死,若去掉则可能无法触发 DocumentCompleted 事件。
}
})
);
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
catch (Exception exception)
{
Response.Write(exception.Message);
}
}
void wb_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
myCounts = new List<MyCount>();
if (wb.ReadyState == WebBrowserReadyState.Complete && wb.IsBusy == false)
{
HtmlDocument doc = wb.Document; //抓取网页
HtmlElement hem = doc.GetElementById("list");//这里就像js里面一样通过ID来查找对象
for (int i = 0; i < hem.Children.Count; i++)
{
string innertext = hem.Children[i].InnerText.Trim();
string[] temps = innertext.Split(' ');
myCounts.Add(new MyCount() { Name = temps[0], Number = temps[1] });
}
}
else
{
Response.Write(wb.ReadyState.ToString());
}
}
