您的位置:首页 > 其它

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

2011-04-29 18:05 399 查看
阅读全文下载例子:http://www.cckan.net/forum.php?mod=viewthread&tid=370

有段时间不写博客了,最近工作压力比较大,大家在忙什么,新近安装了Win7的操作系统,感觉很不错,还体验了一把IE9,里面的开发人员工具很好用

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

我们先来看看下面的图片吧



在hao123的这个界面里我们只要输入一个手机号不管是移动,联通,电信的都可以,单击查询就可以直接查询到归属地,和号码类型,网上这样的

网站很多,我就以这个为例子吧,那我们怎么样把这些信息放到我们自己的网站上呢?

我们先来分析一下,其实很方便,我们在IE9下打开这个界面然后在工具---开发人员工具,或是直接安f12也是一样的效果,我们安界面定位到如下图



我们先单击网络然后单击开始捕获,这个时候我们再单击一下查询按钮看看会出现什么情况



是不是出现两个整个,第一个很明显是加载我们所输入号归属地信息,第一个是加载一个图片,对我们没有任何用处,这里不管它,现在我们

来单击一下第一个方法看看捕获到了什么



URL http://vip.showji.com/locating/?m=13888888888&outfmt=json&callback=phone.callBack 很明显这是一个GEt请求,只要请求这个地址就能得到下面的结果

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

用手机号,省,市,还有邮编,号码类型等信息。这样看的话是不是我们直接把这个地区复制到地址栏里就行了,那咱们一起来看看效果吧



果然没错就是我们想要的东西,大家别急,其它还可以更简单,我们来看一下这个URL

http://vip.showji.com/locating/?m=13888888888&outfmt=json&callback=phone.callBack

如果现在我把这个RUles号码后面的删除只保留http://vip.showji.com/locating/?m=13888888888这些会出现什么情况呢?

直接放到地址栏里试试效果



呵呵,很神奇吧,居然得到的是一个Xml文件

这就像是我们在调用WebServces一样简单了,我们只要写一个程序请求这个地址就可以得到我们想要的效果了。

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

我就不一步一步的分析了大家直接看我的代码

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, 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();

}

/// <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, 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();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: