http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 3499|回复: 1

帮忙修改下多线程操作函数。。。

[复制链接]
发表于 2013-10-23 12:49:06 | 显示全部楼层 |阅读模式
提需求
联系方式: 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个线程执行抓取网页?




1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2013-10-24 18:02:50 | 显示全部楼层
这个应该先把代理IP检测一遍,之后再分配IP并采集网页....
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-10 16:22

© 2014-2021

快速回复 返回顶部 返回列表