时间:2021-05-19
采集器概貌,如下:
最近做一个项目,功能类似于CNZZ站长统计功能,要求显示Ip所在的省份市区/提供商等信息。网上的Ip纯真数据库,下载下来一看,发现没提供商内容,省市区都很少,居然有XXX网吧,哥瞬间倒了。没标准化、并且杂乱、还不连续的IP段、总体说来没达到要求。
在百度上找啊找,找到淘宝Ip地址库,官方介绍的相当诱人,准确率高,数据质量有保障,提供国家、省、市、县、运营商全方位信息,信息维度广,格式规范,但是限制每秒10次的访问(这个比较无语)。
淘宝IP地址库,提供API http://ip.taobao.com/
接口说明
1. 请求接口(GET):
http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址字串]
2. 响应信息:
(json格式的)国家 、省(自治区或直辖市)、市(县)、运营商
3. 返回数据格式:
{"code":0,"data":{"ip":"210.75.225.254","country":"\u4e2d\u56fd","area":"\u534e\u5317",
"region":"\u5317\u4eac\u5e02","city":"\u5317\u4eac\u5e02","county":"","isp":"\u7535\u4fe1",
"country_id":"86","area_id":"100000","region_id":"110000","city_id":"110000",
"county_id":"-1","isp_id":"100017"}}
其中code的值的含义为,0:成功,1:失败。
1 :IP转换
准备好工具,后面就好弄啦, IPHelper提供了各种,IP<->byte[]<->Long 转换
复制代码 代码如下:
public class IPHelper
{
/// <summary>
/// ip转成long
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static long Ip2Long(string ip)
{
byte[] bytes = Ip2Bytes(ip);
return Bytes2Long(bytes);
}
/// <summary>
/// long转成ip
/// </summary>
/// <param name="ipLong"></param>
/// <returns></returns>
public static string Long2Ip(long ipLong)
{
byte[] bytes = Long2Bytes(ipLong);
return Bytes2Ip(bytes);
}
/// <summary>
/// long转成byte[]
/// </summary>
/// <param name="ipvalue"></param>
/// <returns></returns>
public static byte[] Long2Bytes(long ipvalue)
{
byte[] b = new byte[4];
for (int i = 0; i < 4; i++)
{
b[3 - i] = (byte)(ipvalue >> 8 * i & 255);
}
return b;
}
/// <summary>
/// byte[]转成long
/// </summary>
/// <param name="bt"></param>
/// <returns></returns>
public static long Bytes2Long(byte[] bt)
{
int x = 3;
long o = 0;
foreach (byte f in bt)
{
o += (long)f << 8 * x--;
}
return o;
}
/// <summary>
/// ip转成byte[]
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static byte[] Ip2Bytes(string ip)
{
string[] sp = ip.Split('.');
return new byte[] { Convert.ToByte(sp[0]), Convert.ToByte(sp[1]), Convert.ToByte(sp[2]), Convert.ToByte(sp[3]) };
}
/// <summary>
/// byte[]转成ip
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string Bytes2Ip(byte[] bytes)
{
return string.Format("{0}.{1}.{2}.{3}"
, bytes[0]
, bytes[1]
, bytes[2]
, bytes[3]);
}
}
2 :多线程疯狂获取IP
复制代码 代码如下:
/// <summary>
/// 描述:开始采集
/// </summary>
private void StratCollect()
{
foreach (Thread thread in ThreadList)
{
thread.Start();
}
}
/// <summary>
/// 描述:获取要采集的ip long
/// </summary>
private long GetCurrentIp()
{
long curip = System.Threading.Interlocked.Increment(ref CurrentCollectIP);
return curip;
}
/// <summary>
/// 线程中采集的方法
/// </summary>
private void GetTaobaoData()
{
long currentipLong = GetCurrentIp();
while (currentipLong <= EndIP)
{
try
{
CaptureTaobaoIPData(currentipLong);
}
catch (Exception ex)
{
TextLog.SetString(currentipLong + ex.Message);
}
currentipLong = GetCurrentIp();
}
}
/// <summary>
/// 描述:线程中采集并得到IP
/// </summary>
private void CaptureTaobaoIPData(long currentipLong)
{
string ip = IPHelper.Long2Ip(currentipLong);
string url = string.Format(UrlFomat, ip);
string js =HttpHelper. HttpRequest(url, Encoding.UTF8);
taobaoIPdata m = Newtonsoft.Json.JsonConvert.DeserializeObject<TaobaoJsonData>(js).data;
m.ipLong = currentipLong;
//更新界面
this.Invoke(new Action<taobaoIPdata>(v =>
{
taobaoIPdataList.Add(v);
this.dgv.DataSource = taobaoIPdataList;
}), m);
}
3: Http请求的Json结果,并反序列化成对象
http请求这个相当简单。网上一大把,这里主要说一下json序列化,在这里本人建议采用Newtonsoft.Json.dll 下载地址: http://json.codeplex.com/ 性能和兼容性达到最好
Http请求
复制代码 代码如下:
public class HttpHelper
{
public static string HttpRequest(string url, Encoding encoding)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 6 * 1000;
request.Method = "GET";
//得到处理结果
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, encoding);
string result = myStreamReader.ReadToEnd();
return result;
}
catch (Exception ex)
{
throw ex;
}
}
}
Json序列化
复制代码 代码如下:
taobaoIPdata m = Newtonsoft.Json.JsonConvert.DeserializeObject<TaobaoJsonData>(js).data;
序列号对象taobaoIPdata
复制代码 代码如下:
/// <summary>
/// 淘宝数据
/// </summary>
public partial class taobaoIPdata
{
private long _ipLong;
/// <summary>
/// IP 长整形
/// </summary>
public long ipLong
{
get { return _ipLong; }
set { _ipLong = value; }
}
private string _ip;
/// <summary>
/// IP地址
/// </summary>
public string ip
{
get { return _ip; }
set { _ip = value; }
}
private string _country;
/// <summary>
/// 国家
/// </summary>
public string country
{
get { return _country; }
set { _country = value; }
}
private string _country_id;
/// <summary>
/// 国家编号
/// </summary>
public string country_id
{
get { return _country_id; }
set { _country_id = value; }
}
private string _area;
/// <summary>
/// 地区
/// </summary>
public string area
{
get { return _area; }
set { _area = value; }
}
private string _area_id;
/// <summary>
/// 地区编号
/// </summary>
public string area_id
{
get { return _area_id; }
set { _area_id = value; }
}
private string _region;
/// <summary>
/// 区域
/// </summary>
public string region
{
get { return _region; }
set { _region = value; }
}
private string _region_id;
/// <summary>
/// 区域编号
/// </summary>
public string region_id
{
get { return _region_id; }
set { _region_id = value; }
}
private string _city;
/// <summary>
///城市
/// </summary>
public string city
{
get { return _city; }
set { _city = value; }
}
private string _city_id;
/// <summary>
/// 城市编号
/// </summary>
public string city_id
{
get { return _city_id; }
set { _city_id = value; }
}
private string _county;
/// <summary>
/// 县
/// </summary>
public string county
{
get { return _county; }
set { _county = value; }
}
private string _county_id;
/// <summary>
/// 县编号
/// </summary>
public string county_id
{
get { return _county_id; }
set { _county_id = value; }
}
private string _isp;
/// <summary>
/// 供应商
/// </summary>
public string isp
{
get { return _isp; }
set { _isp = value; }
}
private string _isp_id;
/// <summary>
/// 供应商ID
/// </summary>
public string isp_id
{
get { return _isp_id; }
set { _isp_id = value; }
}
}
/// <summary>
/// 淘宝api 返回的json数据
/// </summary>
public partial class TaobaoJsonData
{
public int code { get; set; }
public taobaoIPdata data { get; set; }
}
序列号对象taobaoIPdata
4:插入到数据库中。。。剩下的自己随便搞啦
源码下载:淘宝IP获取器.rar
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文所述为C#数据采集器,并结合有数据库操作,比较实用。读者可以进一步再完善一下写成一个更加成熟的数据采集程序。具体功能代码如下:usingSystem;usi
什么是数据采集器手持数据采集器,又称盘点机、掌上电脑。它是将条码扫描装置,RFID技术与数据终端一体化,带有电池可离线操作的终端电脑设备。具备实时采集、自动存储
本文实例讲述了C#获取真实IP地址实现方法,分享给大家供大家参考。具体实现方法如下:通常来说,大家获取用户IP地址常用的方法是:复制代码代码如下:stringI
采集器,通常又叫小偷程序,主要是用来抓取别人网页内容的。关于采集器的制作,其实并不难,就是远程打开要采集的网页,然后用正则表达式将需要的内容匹配出来,只要稍微有
什么是数据采集器手持数据采集器,又称盘点机、掌上电脑。它是将条码扫描装置,RFID技术与数据终端一体化,带有电池可离线操作的终端电脑设备。具备实时采集、自动存储