c 抓取网页数据( 谷歌都行的手机号码归属地查询的 )

优采云 发布时间: 2022-04-02 17:13

  c 抓取网页数据(

谷歌都行的手机号码归属地查询的

)

  

  

  【转载】提供网页抓取hao123手机号归属地示例-苏飞-Perky Su-博客园。

  我有一段时间没有写博客了。最近工作压力很大。你在忙什么?最近装了win7操作系统,感觉很不错。我也体验过IE9。其中的开发人员工具非常有用。

  说到这里,你可以用火狐的谷歌。在这个例子中,我主要使用IE9自带的来分析hao123手机号码归属地查询的问题。

  让我们看看下面的图片

  

  在hao123的这个界面中,我们只需要输入一个手机号码,无论是中国移动、中国联通还是电信,点击查询,就可以直接查询到归属地、号码类型,比如在线。

  网站很多,我就以这个为例,那我们如何把这些信息放在我们自己的网站上呢?

  我们先来分析一下。它实际上非常方便。我们在IE9下打开这个界面然后进入工具-开发者工具,或者直接安装f12。

  

  我们先点击网络然后点击开始抓包,这次我们再次点击查询按钮看看会发生什么

  

  有两个完整的吗?第一个显然是加载我们输入的号码的归属信息,而第一个是加载一张图片,这对我们没有任何用处。我不在乎这里,现在我们

  我们点击第一种方法,看看捕获了什么

  

  URL显然是一个GET请求,只要请求这个地址,就可以得到如下结果

  phone.callBack({"Mobile":"","QueryResult":"True","Province":"云南","City":"昆明","AreaCode":"0871","PostCode":"650000 ","企业":"中国移动","卡":"GSM"});

  使用手机号、省、市,以及邮编、号码类型等信息。如果我们这样看,我们可以直接把这个区域复制到地址栏中,然后我们来看看效果。

  

  果然是我们想要的,别着急,其他的可以更简单,我们来看看这个网址

  如果我删除以下 RUles 编号并仅保留这些编号会怎样?

  直接放到地址栏试试效果

  

  哦,太神奇了,我得到的是一个xml文件

  这就像我们调用WebServices一样简单,我们只需要编写一个程序来请求这个地址就可以得到我们想要的效果。

  随便新建一个项目,一起看看

  我就不一步一步分析了,你可以直接看我的代码

  

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)

{

}

这个方法在这里没有用到,大家可以做为参考

///

/// 传入URL返回网页的html代码

///

///

URL ///

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, 0, 256);

while (count > 0)

{

String tempStr = new String(sReaderBuffer, 0, count);

content.Append(tempStr);

count = sReader.Read(sReaderBuffer, 0, 256);

}

// 读取结束

sReader.Close();

}

catch (Exception)

{

content = new StringBuilder("Runtime Error");

}

return content.ToString();

}

///

/// 好123查询,符合下列规则也可使用

/// 返回xml

/// 需要顺序的节点:

/// QueryResult(查询结果状态True,False)

/// Province(所属省份)

/// City(所属地区)

/// Corp(服务商)

/// Card(卡类型 GSM)

/// AreaCode(区号)

/// PostCode(邮编)

///

///

///

///

public static string[] GetInfoByxml(string url, string mobileNum)

{

try

{

XmlDocument xml = new XmlDocument();

// xml.LoadXml("

15890636739True

河南郑州0371

450000中国移动GSM");

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也是一样,把我的方法复制到你网页的cs代码中就OK了。

  好了,我们的分析到此结束。

  这里我再添加一个调用网站的方法,带证书到大空

  因为证书文件要通过证书文件进行验证,所以我们直接让他在本地回调验证。在这种情况下,我们需要重写方法。我们来看看回调方法。

  

//回调验证证书问题

public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)

{ // 总是接受

return true;

}

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

  

///

/// 传入URL返回网页的html代码

///

///

URL ///

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, 0, 256);

while (count > 0)

{

String tempStr = new String(sReaderBuffer, 0, count);

content.Append(tempStr);

count = sReader.Read(sReaderBuffer, 0, 256);

}

// 读取结束

sReader.Close();

}

catch (Exception)

{

content = new StringBuilder("Runtime Error");

}

return content.ToString();

}

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线