请高手告诉我,哪里的问题,怎么解决,谢谢啦
大概就是去百度搜索一个关键词,商业原因,没给出具体的关键词
然后看看百度上会不会因为这个关键词出现一些数据只是给了个大概,请大家看一下,为什么占用内存越来越多,怎么处理?
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文本数据
}
大概就是去百度搜索一个关键词,商业原因,没给出具体的关键词
然后看看百度上会不会因为这个关键词出现一些数据只是给了个大概,请大家看一下,为什么占用内存越来越多,怎么处理?
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文本数据
}
GC.Collect();根据垃圾收集的策略是这样的
如果在调用GC的时候.....内容很多.... -_-
去掉试试看先 也可能不是这个原因
另外为什么要不断地new一个出来
不能公用一个吗....
怀疑是不是这个问题,OpenNew?
干嘛用的?
网页源码都获得了,直接分析不就行了?
这句代码能大幅降低内存使用,有什么副作用就不清楚了
{
flag = false;
}
看看这个代码是不是执行,要是不执行的话就是死循环
这句果然好用……可是谁知道原因啊?再等等看,实在没人知道就结了啊
WebBrowser browser = new WebBrowser();
这个你反复执行没有必要。声明为字段,每次直接用可以了。