今天使用C#技术实现从几个不同的网站批量查询IP归属地。为什么从不同的网站去爬取IP归属地结果呢?因为某些时候个别网站无法访问,那么我们就需要使用备用网站在线获取IP归属地结果。

我从下面这几个网站在线获取IP归属地查询数据

1.https://api.devopsclub.cn/api/ipv4query?ip=您的IP
2.https://www.ip.cn/api/index?ip=您的IP&type=1
3.http://tool.114la.com/index.php?ct=site&ac=ip&q=您的IP
4.http://ip.ws.126.net/ipquery?ip=您的IP
有些网站是使用JSON返回IP归属地查询结果有些则直接返回HTML,我们使用Newtonsoft.Json解析JSON返回的IP归属地查询结果、利用HtmlAgilityPack解析网页返回的IP归属地查询结果。
public static IpInfo GetIpLocal(string ip)
{
var url = "https://api.devopsclub.cn/api/ipv4query?ip=" + ip;
var responsInfo = HttpRequest.Get.SendGetMoreInfo(url);
IpInfo ipInfo = null;
if (responsInfo.Success)
{
    var returnData = responsInfo.Content.ToDynamic();
    ipInfo = Converts.ConvertDynamic.ToObject<IpInfo>(returnData["data"]);
}
if (ipInfo == null || ipInfo.Address.IsNullOrEmpty())
{
    url = "https://www.ip.cn/api/index?ip=" + ip + "&type=1";
    responsInfo = HttpRequest.Get.SendGetMoreInfo(url);
    if (responsInfo.Success)
    {
        var returnData = responsInfo.Content.ToDynamic();
        var address = returnData["address"];
        if (address != null) ipInfo = new IpInfo() { Region = (string)address };
    }
}

if (ipInfo == null || ipInfo.Address.IsNullOrEmpty())
{
    url = "http://tool.114la.com/index.php?ct=site&ac=ip&q=" + ip;
    responsInfo = HttpRequest.Get.SendGetMoreInfo(url);
    if (responsInfo.Success)
    {
        HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(responsInfo.Content);
        var addressNode = doc.DocumentNode.SelectSingleNode("//*[@id=\"info\"]/tr[3]/td");
        if (addressNode != null)
        {
            ipInfo = new IpInfo() { Region = (string)addressNode.InnerText };
        }
    }
}
if (ipInfo == null || ipInfo.Address.IsNullOrEmpty())
{
    url = "http://ip.ws.126.net/ipquery?ip=" + ip;
    responsInfo = HttpRequest.Get.SendGetMoreInfo(url);
    if (responsInfo.Success)
    {
        var provinceValue = responsInfo.Content.RegexValue("province:\"(.*?)\"");
        var cityValue = responsInfo.Content.RegexValue("city:\"(.*?)\"");
        if (!provinceValue.IsNullOrEmpty() || !cityValue.IsNullOrEmpty())
        {
            ipInfo = new IpInfo() { City = cityValue, Province = provinceValue };
        }
    }
}
if (ipInfo == null) ipInfo = new IpInfo() { };
return ipInfo;
}
利用一些网站在线提供的IP归属地查询功能,我们便可以轻松的使用C#语言编写爬虫去是实现IP归属地批量查询的功能。

本文C#查询IP归属地 批量爬取网站IP归属地查询结果it boy酷学软件发布的原创文章
转载请注明地址:http://www.pupilshome.com/blogs/201212.html