|
提需求
联系方式: |
sufeinet@sufeinet.com |
金额: |
1 元 |
启动多线程:
[code=csharp] OutputInfo("开始启动操作线程...");
for (int i = 0; i <= ThreadNumber; i++)
{
ThreadStart threadStart = new ThreadStart(StartGo);
thread = new Thread(threadStart);
thread.Name = i.ToString();
ThL.Add(thread);//线程列表。用于终止线程
thread.Start();
}[/code]
函数:
[code=csharp] private void StartGo()
{
string URL = "";
string proxy = "";
string htmlcode = "";
while (Proxylist.Count != 0)
{
for (int i = 0; i < Proxylist.Count; i++)//循环代理
{
lock (tLock)
{
if (URL == "")
{
URL = Linkslist[0];//链接列表
Linkslist.Remove(URL);
}
proxy = Proxylist[0];//代理列表
Proxylist.Remove(proxy);
Proxy_label.Text = Proxylist.Count.ToString();
}//loo
htmlcode = GetHtmlCode(URL, proxy);//取得网页代码
OutputInfo("线程[ " + Thread.CurrentThread.Name + " ] 操作" + proxy + " / " + GetViewkey(URL));
if (htmlcode.Length < 30)
{
OutputInfo("错误:" + proxy + " > " + htmlcode);
proxy = "";
}
else if (GetContent(1, htmlcode) == "")
{
OutputInfo("错误:" + proxy + "失效,继续下一个代理! > ");
proxy = "";
}
else
{
ModData(GetContent(1, htmlcode), GetContent(0, htmlcode), GetViewkey(htmlcode).Trim());//存入数据库
proxy = "";
// Linkslist.Remove(URL);
LinksData_label.Text = Linkslist.Count.ToString();
URL = "";
}
}//for
}//w
OutputInfo("代理地址已经使用完!");
}[/code]
这是采集网站用的。我的思路是:循环代理地址取网页代码。
一条代理取一个网页。
代理能用。成功取网页。下个代理下个网页继续。
不能用。下个代理继续抓取这个网页。
能不能把代码修改成。比如:
线程1执行1~101的代理
线程2执行101~201的代理
.。。。。。
这样平均分配给5个线程执行抓取网页?
|
|