基本信息
源码名称:C#百度指数抓取方法(2012年版本,已失效 学习思路即可)
源码大小:0.09M
文件格式:.zip
开发语言:C#
更新时间:2015-07-26
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

C#监听webBrowser包来抓取到百度指数


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;

namespace test
{
    /// <summary>
    /// 
    /// </summary>
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //定义一个webBrowser对象用来查询百度信息
        System.Windows.Forms.WebBrowser webBrowser1 = new WebBrowser();

        //在加载时处理数据
        private void Form1_Load(object sender, EventArgs e)
        {
            //加载本机的所有IP地址
            IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
            foreach (IPAddress ipad in ips)
            {
                comboBox1.Items.Add(ipad.ToString());
            }

            #region 初始化webBrowser1

            webBrowser1.Location = new System.Drawing.Point(5, 38);
            webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
            webBrowser1.Name = "webBrowser1";
            webBrowser1.Size = new System.Drawing.Size(855, 279);
            webBrowser1.TabIndex = 5;

            #endregion
        }
        Boolean F = true;
        private void button1_Click(object sender, EventArgs e)
        {
            //清空当前数据
            richTextBox1.Text = "";

            //存数据的变量
            string userIndexes = "";

            //是否开始捕获数据
            F = true;

            //开始查询百度信息
            webBrowser1.Navigate("http://index.baidu.com/main/word.php?word="   textBox1.Text.Trim());

            //创建一个Socket对象
            Socket s = new Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Raw, System.Net.Sockets.ProtocolType.IP);

            //绑定IP到Socket
            s.Bind(new System.Net.IPEndPoint(IPAddress.Parse(comboBox1.Text), 0));

            //为当前数据报提供标头
            s.SetSocketOption(System.Net.Sockets.SocketOptionLevel.IP, System.Net.Sockets.SocketOptionName.HeaderIncluded, 1);

            //检查电脑所有端口
            s.IOControl(unchecked((int)0x98000001), new byte[4] { 1, 0, 0, 0 }, new byte[4]);

            //下面的线程只执行8秒,8秒后就不再执行了。
            timer1.Enabled = true;

            //开始一个新的线程,查询数据
            Thread th = new Thread(delegate()
            {
                try
                {
                    //确定端口 让端口唯一是为了防止多个用户同时查询时的串数据
                    string port = "";

                    //是否开始抓包
                    while (F)
                    {
                        //获取从网络中得到的数据,看是否有可读的数据
                        if (s.Available > 0)
                        {
                            //计算机所有端口
                            byte[] bs = new byte[65536];

                            //将接受到的数据存储的缓冲共里面
                            s.Receive(bs);
                            //创建一个IPIPPacket
                            IPPacket ip = new IPPacket(ref bs);

                            //只有当接发送方使用TCP协议,并且源IP为180.149.131.33时才执行
                            if (ip.TCP != null && ip.SourceAddress.ToString().Trim() == Dns.GetHostByName("index.baidu.com").AddressList[0].ToString().Trim())
                            {
                                //将缓冲区中的数据以指定格式进行筛选,并把结果存储到Data中
                                string Data = System.Text.RegularExpressions.Regex.Replace(System.Text.Encoding.ASCII.GetString(ip.TCP.PacketData),
                                    @"[^a-zA-Z_0-9\.\@\- ]", "");

                                //开始一次取数据时的报文端口号
                                if (Data.Contains("userIndexes"))
                                {
                                    port = ip.TCP.DestinationPort.ToString().Trim();
                                }

                                //一次存储取出的数据
                                userIndexes  = Data;

                                //当出现mediaIndexes时结束捕获
                                if (Data.Contains("mediaIndexes") && (port == "" || port == ip.TCP.DestinationPort.ToString().Trim()))
                                {
                                    //不在捕获的标志
                                    F = false;

                                    //关闭当前Socket
                                    s.Close();

                                    //取出userIndexes之后的数据
                                    userIndexes = userIndexes.Substring(userIndexes.IndexOf("userIndexes"));

                                    //取出mediaIndexes之前的数据
                                    userIndexes = userIndexes.Substring(0, userIndexes.IndexOf("mediaIndexes")   12);
                                }

                                //取当前流的端口
                                port = ip.TCP.DestinationPort.ToString().Trim();
                            }
                        }
                    }

                    //把最终取出的数据放入richTextBox1中去
                    if (userIndexes.Contains("mediaIndexes"))
                        richTextBox1.Text = userIndexes;
                    else
                        richTextBox1.Text = "数据不存在";
                }
                catch (Exception ex)
                {
                    F = false;
                    richTextBox1.Text = ex.Message.Trim();
                }
            });
            th.Start();
        }

        //在关闭窗体前结束进程
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            F = false;
        }
    }
}