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

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

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

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

查看: 32990|回复: 51
打印 上一主题 下一主题

[抓取] 自己写的百度搜索结果分析程序,上来和大家分享下

[复制链接]
跳转到指定楼层
楼主
发表于 2014-9-5 08:48:07 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
我本来是搞php开发的,但是来了公司后需要做一个百度搜索的结果分析软件,我开始的时候已经用php开发玩了,但是后面在使用的过程中才发现php做的网页版的,查询起来特别不方便,所以后来才决定自学下C#,搞搞桌面开发,到今天自学C#差不多有快一个月的时间了。下面说下我主要做的东西
首先是软件的主界面:由于是给公司内部使用的,所以我对软件做了加密处理,还有注册码的添加。
下面就是主要的百度搜索结果分析的功能,我之前自己再网上拔下来不少代码,然后自己拼凑到一起,已经开发完成了一个查词的功能,但是做完才发现这东西必须要用多线程来处理,要不然界面就直接卡死了。但是多线程网上可不像我之前看基础教程那样有视频,所以自学了快一周了也没有搞明白。



下面是我主要的代码,里面请求HTTP网页信息的代码是我自己从网上拔下来,然后自己改吧改吧的。
/// <summary>
        /// 搜索处理
        /// </summary>
        /// <param name="url">搜索网址</param>
        /// <param name="Chareset">编码</param>
        public string search(string url, string Chareset)
        {
            string yinqing = (string)comboBoxYinqing.SelectedItem;
            string resulthtml = "";
            System.GC.Collect();
            //设置系统默认的连接数
            System.Net.ServicePointManager.DefaultConnectionLimit = 1000;
            string[] arr = new string[]{"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36 SE 2.X MetaSr 1.0",
                "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)",
                "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)",
                "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Win64; x64; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)"};
            Random rd = new Random();//创建随机数的对象
            rd.Next(arr.Length);// 是只取出来的数字 是在 0到数组arr长度之间的任意数字
            string UserAgent = "";
            if (yinqing == "百度手机搜索引擎移动网络")
            {
                UserAgent = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3 like Mac OS X; de-de) AppleWebKit/533.17.9 (KHTML, like Gecko) Mobile/8F190";
            }
            else
            {
                UserAgent = arr[rd.Next(arr.Length)];// 把随机抽出来的索引所对应数组的值赋给textBox3文本
            }

            HttpState result = new HttpState();

            long time = CommonHelper.ConvertDateTimeInt(DateTime.Now);
            //Uri uri = new Uri(url + "&yfsj =" + time);
            Uri uri = new Uri(url);
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(uri);

            //System.Net.WebProxy proxy = new WebProxy("180.97.33.67", 80);
            //System.Net.WebProxy proxy = new WebProxy("192.168.1.1", 80);
            //myHttpWebRequest.Proxy = proxy;

            //myHttpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
            //myHttpWebRequest.Headers.Set("Content-Encoding", "gzip");
            //myHttpWebRequest.Headers.Add("Accept-Language: zh-cn;q=0.5");
            //myHttpWebRequest.Headers.Add("UA-CPU: x86");

            myHttpWebRequest.ServicePoint.Expect100Continue = false;
            myHttpWebRequest.ServicePoint.UseNagleAlgorithm = false;//是否使用 Nagle 不使用 提高效率
            myHttpWebRequest.ServicePoint.ConnectionLimit = 65500;//最大连接数
            myHttpWebRequest.AllowWriteStreamBuffering = false;//数据是否缓冲 false 提高效率  

            myHttpWebRequest.UseDefaultCredentials = true;
            myHttpWebRequest.ContentType = "text/html";
            myHttpWebRequest.UserAgent = UserAgent;
            myHttpWebRequest.Method = "GET";
            myHttpWebRequest.Accept = "        text/html, */*; q=0.01";
            //myHttpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            //myHttpWebRequest.ContentLength = 100000000;//数据出错了,太大了

            //保持连接
            myHttpWebRequest.KeepAlive = false;
            myHttpWebRequest.ProtocolVersion = HttpVersion.Version10;

            myHttpWebRequest.Host = "www.baidu.com";
            myHttpWebRequest.Referer = myHttpWebRequest.Address.AbsoluteUri;

            //myHttpWebRequest.CookieContainer = new CookieContainer();
            myHttpWebRequest.CookieContainer = baiduwebrequest.CookieContainer;
            //超时设置
            myHttpWebRequest.Timeout = 1 * 30 * 1000;//设置超时3秒

            HttpWebResponse response = null;
            try
            {
                System.GC.Collect();
                response = (HttpWebResponse)myHttpWebRequest.GetResponse();
                // 从 ResponseStream 中读取HTML源码并格式化 add by cqp
                result.Html = readResponseStream(response, Chareset);
                result.CookieContainer = myHttpWebRequest.CookieContainer;
                //查询完成后关闭连接
                //myHttpWebRequest = null;
                //response.Close();
                //response = null;
                resulthtml = result.Html;
                return resulthtml;
            }
            catch (Exception ex)
            {
                //MessageBox.Show("网络连接出现问题,请检查网络" + ex.ToString());
                //Application.DoEvents();
                toolStripStatusLabelTishi.Text = "提示:网络连接出现问题,请检查网络,正在重试!或者你可以更换ip";
                if (myHttpWebRequest != null)
                {
                    myHttpWebRequest.Abort();
                    myHttpWebRequest = null;
                }
                //search(url, "utf-8");
                return ex.ToString();
            }

        }

        public string readResponseStream(HttpWebResponse response, string Chareset)
        {
            string result = "";
            System.IO.Compression.DeflateStream responseStream = new System.IO.Compression.DeflateStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
            using (StreamReader responseReader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(Chareset)))
            {
                result = formatHTML(responseReader.ReadToEnd());
                responseReader.Close();
                responseReader.Dispose();
                response.Close();
            }

            return result;
        }
        /// <summary>
        /// 描述:格式化网页源码
        ///
        /// </summary>
        /// <param name="htmlContent"></param>
        /// <returns></returns>
        public string formatHTML(string htmlContent)
        {
            string result = "";

            result = htmlContent.Replace("&#187;", "").Replace(" ", "")
                    .Replace("&#169;", "").Replace("\r", "").Replace("\t", "")
                    .Replace("\n", "").Replace("&", "").Replace("\r\n", "")
                    .Replace("<em>", "").Replace("</em>", "")
                    .Replace("<EM>", "").Replace("</EM>", "");
            result = Regex.Replace(result, "<script.*?</script>", "");
            result = Regex.Replace(result, "<style.*?>.*?</style>", "");
            result = Regex.Replace(result, "<a.*?>", "");
            result = Regex.Replace(result, "<img.*?>", "");
            return result;
        }


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
沙发
发表于 2014-9-5 08:50:01 | 只看该作者
我只是路过打酱油的。
板凳
 楼主| 发表于 2014-9-5 08:50:56 | 只看该作者
站长苏飞 发表于 2014-9-5 08:50
我只是路过打酱油的。

求助要站长,我用不了你的httphelper的类呀,也不知道多线程怎么弄
地板
发表于 2014-9-5 09:09:32 | 只看该作者
用不了说清楚,怎么用不了
5
 楼主| 发表于 2014-9-5 10:05:25 | 只看该作者
站长苏飞 发表于 2014-9-5 09:09
用不了说清楚,怎么用不了

第一:就是不知道怎么调用,还有怎么返回我要查询的网页内容
第二:怎么用你的类来进行多线程查询的开发,一点思路都没有
所以希望帅哥哥提供个查询的例子
6
发表于 2014-9-5 10:12:02 | 只看该作者
分析排名?还是其它
7
 楼主| 发表于 2014-9-5 10:42:59 | 只看该作者
分析排名的
8
发表于 2014-9-5 10:46:05 | 只看该作者
我只是路过打酱油的。
9
发表于 2014-9-5 10:47:25 | 只看该作者
yq5858588 发表于 2014-9-5 10:05
第一:就是不知道怎么调用,还有怎么返回我要查询的网页内容
第二:怎么用你的类来进行多线程查询的开发 ...

http://www.sufeinet.com/forum.ph ... on=view&ctid=26
10
发表于 2014-9-5 10:59:57 | 只看该作者
站长不是有一个多线程的例子么
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

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

GMT+8, 2024-4-20 20:51

© 2014-2021

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