提供一个网页抓取hao123手机号码归属地的例子

来源:互联网 发布:网络上p是什么意思 编辑:程序博客网 时间:2024/04/29 12:47

利用这个地址http://vip.showji.com/locating/?m=任意的手机号就可以查看此手机号码归属地:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Security.Cryptography;
using System.Xml;

namespace ccbText
{
    
public partial class Form2 : Form
    {

        
public Form2()
        {
            InitializeComponent();
        }

        
private void Form2_Load(object sender, EventArgs e)
        {
        }
      
       这个方法在这里没有用到,大家可以做为参考
        
/// <summary>
        
/// 传入URL返回网页的html代码
        
/// </summary>
        
/// <param name="Url">URL</param>
        
/// <returns></returns>
        public string GetUrltoHtml(string Url)
        {
            StringBuilder content 
= new StringBuilder();

            
try
            {
                
// 与指定URL创建HTTP请求
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.KeepAlive 
= false;
                
// 获取对应HTTP请求的响应
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                
// 获取响应流
                Stream responseStream = response.GetResponseStream();
                
// 对接响应流(以"GBK"字符集)
                StreamReader sReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
                
// 开始读取数据
                Char[] sReaderBuffer = new Char[256];
                
int count = sReader.Read(sReaderBuffer, 0256);
                
while (count > 0)
                {
                    String tempStr 
= new String(sReaderBuffer, 0, count);
                    content.Append(tempStr);
                    count 
= sReader.Read(sReaderBuffer, 0256);
                }
                
// 读取结束
                sReader.Close();
            }
            
catch (Exception)
            {
                content 
= new StringBuilder("Runtime Error");
            }

            
return content.ToString();

        }

        
/// <summary>
        
/// 好123查询,符合下列规则也可使用
        
/// 返回xml
        
/// 需要顺序的节点:
        
/// QueryResult(查询结果状态True,False)
        
/// Province(所属省份)
        
/// City(所属地区)
        
/// Corp(服务商)
        
/// Card(卡类型 GSM)
        
/// AreaCode(区号)
        
/// PostCode(邮编)
        
/// </summary>
        
/// <param name="url"></param>
        
/// <param name="mobileNum"></param>
        
/// <returns></returns>
        public static string[] GetInfoByxml(string url, string mobileNum)
        {
            
try
            {
                XmlDocument xml 
= new XmlDocument();
                
// xml.LoadXml("<?xml version='1.0' encoding='utf-8' ?><QueryResponse xmlns='http://api.showji.com/Locating/'><Mobile>15890636739</Mobile><QueryResult>True</QueryResult><Province>河南</Province><City>郑州</City><AreaCode>0371</AreaCode><PostCode>450000</PostCode><Corp>中国移动</Corp><Card>GSM</Card></QueryResponse>");
                xml.Load(string.Format(url, mobileNum));
                XmlNamespaceManager xmlNm 
= new XmlNamespaceManager(xml.NameTable);
                xmlNm.AddNamespace(
"content""http://api.showji.com/Locating/");
                XmlNodeList nodes 
= xml.SelectNodes("//content:QueryResult|//content:Mobile|//content:Province|//content:City|//content:Corp|//content:Card|//content:AreaCode|//content:PostCode", xmlNm);
                
if (nodes.Count == 8)
                {
                    
if ("True".Equals(nodes[1].InnerText))
                    {

                        
return new string[] { nodes[0].InnerText, nodes[2].InnerText + nodes[3].InnerText, nodes[6].InnerText + nodes[7].InnerText, nodes[4].InnerText, nodes[5].InnerText };
                    }
                }
                
return new string[] { "FALSE" };
            }
            
catch
            {
                
return new string[] { "FALSE" };
            }
        }

        
//调用方法查询数据
        private void button1_Click(object sender, EventArgs e)
        {
            
foreach (string item in GetInfoByxml(" http://vip.showji.com/locating/?m={0}", txtMobile.Text.Trim()))
            {
                richTextBox1.Text 
+= "__" + item;
            }
        }
    }
}

 

 

运行一下看看效果吧

 

我是用Winfrom做的测试,大家如果想用Asp。Net也是一样的,把我的方法复制到你的Web页面的Cs代码下就OK了。

好了我们的分析到这里就算是结束了,

在这里我再给大空补充一个关于调用带有证书的网站的调用 方法

因为带证书的都是在要验证证书文件的,我们在这里直接让他在本地回调验证,这样的话就要吧重写方法了,下在看一下回调的方法吧

 

 

  //回调验证证书问题
        public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {   
// 总是接受    
            return true;
        }

 

其它很简单只要在 我们上面的方法GetUrltoHtml()中加入几行代码就行了,修改后的方法

 

 

 /// <summary>
        
/// 传入URL返回网页的html代码
        
/// </summary>
        
/// <param name="Url">URL</param>
        
/// <returns></returns>
        public string GetUrltoHtml(string Url)
        {
            StringBuilder content 
= new StringBuilder();
            
try
            {
                
// 与指定URL创建HTTP请求
                ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);//验证

                HttpWebRequest request 
= (HttpWebRequest)WebRequest.Create(Url);
                request.KeepAlive 
= false;
                request.UserAgent 
= "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; MS-RTC LM 8; .NET4.0C; .NET4.0E)";
                request.Method 
= "GET";
                request.Accept 
= "*/*";

                
//创建证书文件
                X509Certificate objx509 = new X509Certificate(Application.StartupPath + "\\123.cer");

                
//添加到请求里
                request.ClientCertificates.Add(objx509);

                HttpWebResponse response 
= (HttpWebResponse)request.GetResponse();
                
// 获取对应HTTP请求的响应
                
// 获取响应流
                Stream responseStream = response.GetResponseStream();
                
// 对接响应流(以"GBK"字符集)
                StreamReader sReader = new StreamReader(responseStream, Encoding.GetEncoding("GBK"));
                
// 开始读取数据
                Char[] sReaderBuffer = new Char[256];
                
int count = sReader.Read(sReaderBuffer, 0256);
                
while (count > 0)
                {
                    String tempStr 
= new String(sReaderBuffer, 0, count);
                    content.Append(tempStr);
                    count 
= sReader.Read(sReaderBuffer, 0256);
                }
                
// 读取结束
                sReader.Close();
            }
            
catch (Exception)
            {
                content 
= new StringBuilder("Runtime Error");
            }

            
return content.ToString();

        }