110,534
社区成员
发帖
与我相关
我的任务
分享
public Form1()
{
InitializeComponent();
this.Visible = true;
bool flag = true;
while (flag)
{
HtmlDocument doc;
using (System.Net.WebClient wc = new System.Net.WebClient())
{
wc.Encoding = System.Text.Encoding.UTF8;
string url = "http://www.baidu.com";
string html = wc.DownloadString(url);
convertString2Html(html, out doc);
HtmlElement element = doc.GetElementById("search");
HtmlElementCollection elements = element.GetElementsByTagName("DIV);
string html2 = elements[0].GetAttribute("href").Replace("about:", "");
html = wc.DownloadString(url + html2);
convertString2Html(html, out doc);
element = doc.GetElementById("div1");
elements = element.GetElementsByTagName("p");
elements = elements[0].GetElementsByTagName("span");
if (elements[0].InnerText != "我的天啊")
{
flag = false;
}
url = null;
html = null;
html2 = null;
element = null;
elements = null;
doc = null;
GC.Collect();
}
//System.Threading.Thread.Sleep(30000);
}
System.Media.SoundPlayer player = new SoundPlayer(@"C:\Windows\Media\tada.wav");
player.Load();
player.PlayLooping();
}
private void convertString2Html(string html, out HtmlDocument doc)
{
WebBrowser browser = new WebBrowser();
browser.Navigate("about:blank");
browser.ScriptErrorsSuppressed = true; //屏蔽一些脚本错误
doc = browser.Document.OpenNew(true);
doc.Write(html); //写入采集过来的html文本数据
}