需要在WebFrom中实现一个网页抓取功能,里面用到了System.Windows.Forms.WebBrowser控件,功能已经实现,用VS IISExpress运行没问题,可以取到数据。但一部署到IIS上就出问题了,请各位大神帮忙指导一下,谢谢!
核心代码
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());
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1978/viewspace-3123444/ ,如需转载,请注明出处,否则将追究法律责任。