函数获得网页内容,异步操作

lilunlil 2019-03-23 06:46:46
想写一下函数,用于获得网页源码(后期用正则表达式提取内容),
现在遇到一点问题,就是WebBrowser 必须要加载完成才能获得源码,未完成前为空,
需要用到异步,本人基础薄弱,请高手指点。

以下是写的源码:

using System.Windows.Forms;

namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
string Html;
public Form1()
{
InitializeComponent();

GetHtml();

MessageBox.Show("这是GetHtml()函数运行后的内容" + Html);

/***
问题:有什么方法可以确保到这一行时Html是有内容的
即:我试过异步方法将GetHtml()异步,但效果不明显,我猜测是
DocumentCompleted事件本身相对于GetHtml就是异步的。

相当于 GetHtml()异步一次,而DocumentCompleted事件又异步一次
*****/
}

private void GetHtml()
{
WebBrowser webBrowser1 = new WebBrowser();
string URL = " http://blog.sina.com.cn/s/blog_af65d54c0102yg85.html ";
webBrowser1.Navigate(URL);

webBrowser1.DocumentCompleted += (object sender1, WebBrowserDocumentCompletedEventArgs e1) =>
{
HtmlElement html = webBrowser1.Document.Body;
string str = html.OuterHtml;
MessageBox.Show("这是网页加载完成后的内容"+str);
webBrowser1 = null;//此句目的是为了防止多次运行
};
}

}
}



可以看出,这段代码运行后会先弹出
MessageBox.Show("这是GetHtml()函数运行后的内容" + Html);
这句代码, 此时Html的内容为空;
然后才弹出
MessageBox.Show("这是网页加载完成后的内容"+str);
这句代码,这才是我要的内容,但我在主函数Form1()中却永远无法获得

非专业人士,异步确实不太懂,求高手给段代码。


...全文
145 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
6lilu9 2019-03-24
  • 打赏
  • 举报
回复
https://blog.csdn.net/luanpeng825485697/article/details/78168162 给另一种思路,获取源码不一定需要webBrowser控件呀, 用这个WebClient ,最简单
liusa1997 2019-03-24
  • 打赏
  • 举报
回复
引用 3 楼 lilunlil 的回复:
while (!finished);这句死循环呢。。

你得看清楚finished是全局变量
lilunlil 2019-03-24
  • 打赏
  • 举报
回复
引用 2 楼 caozhy 的回复:
namespace WindowsFormsApp3 { public partial class Form1 : Form { string Html; bool finished = false; public Form1() { InitializeComponent(); finished = false; GetHtml(); while (!finished); MessageBox.Show("这是GetHtml()函数运行后的内容" + Html); /*** 问题:有什么方法可以确保到这一行时Html是有内容的 即:我试过异步方法将GetHtml()异步,但效果不明显,我猜测是 DocumentCompleted事件本身相对于GetHtml就是异步的。 相当于 GetHtml()异步一次,而DocumentCompleted事件又异步一次 *****/ } private void GetHtml() { WebBrowser webBrowser1 = new WebBrowser(); string URL = " http://blog.sina.com.cn/s/blog_af65d54c0102yg85.html "; webBrowser1.DocumentCompleted += (object sender1, WebBrowserDocumentCompletedEventArgs e1) => { HtmlElement html1 = webBrowser1.Document.Body; string Html = html1.OuterHtml; MessageBox.Show("这是网页加载完成后的内容"+Html); webBrowser1 = null;//此句目的是为了防止多次运行 finished = true; }; webBrowser1.Navigate(URL); } } }
while (!finished);这句死循环呢。。
threenewbee 2019-03-24
  • 打赏
  • 举报
回复
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
string Html;
bool finished = false;
public Form1()
{
InitializeComponent();
finished = false;
GetHtml();
while (!finished);
MessageBox.Show("这是GetHtml()函数运行后的内容" + Html);

/***
问题:有什么方法可以确保到这一行时Html是有内容的
即:我试过异步方法将GetHtml()异步,但效果不明显,我猜测是
DocumentCompleted事件本身相对于GetHtml就是异步的。

相当于 GetHtml()异步一次,而DocumentCompleted事件又异步一次
*****/
}

private void GetHtml()
{
WebBrowser webBrowser1 = new WebBrowser();
string URL = " http://blog.sina.com.cn/s/blog_af65d54c0102yg85.html ";


webBrowser1.DocumentCompleted += (object sender1, WebBrowserDocumentCompletedEventArgs e1) =>
{
HtmlElement html = webBrowser1.Document.Body;
string str = html.OuterHtml;
MessageBox.Show("这是网页加载完成后的内容"+str);
webBrowser1 = null;//此句目的是为了防止多次运行
finished = true;
};
webBrowser1.Navigate(URL);
}

}
}
  • 打赏
  • 举报
回复
既然你不太懂基于事件编程设计,那么怎么又会用 webBrowser1.DocumentCompleted 事件呢?还是把事件搞清楚! 另外,DocumentCompleted 其实并不是保证页面上所有内容都加载完毕,而是页面上各种内容每当加载一个文档结束时就触发一次事件,以内页面加载完毕之前会触发多次(例如20次)此事件。要检测页面是否完全加载完毕,还需要在这个事件处理过程中再去检测 。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧