最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Python程序抓取新浪在國內(nèi)的所有IP的教程

 更新時間:2015年05月04日 17:08:58   投稿:goldensun  
這篇文章主要介紹了使用Python程序抓取新浪在國內(nèi)的所有IP的教程,作為Python網(wǎng)絡(luò)編程中獲取IP的一個小實踐,需要的朋友可以參考下

數(shù)據(jù)分析,特別是網(wǎng)站分析中需要對訪問者的IP進行分析,分析IP中主要是區(qū)分來訪者的省份+城市+行政區(qū)數(shù)據(jù),考慮到目前純真IP數(shù)據(jù)庫并沒有把這些數(shù)據(jù)做很好的區(qū)分,于是尋找了另外一個可行的方案(當(dāng)然不是花錢買哈)。解決方案就是抓取新浪的IP數(shù)據(jù)。

新浪的IP數(shù)據(jù)接口為:
 
http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=123.124.2.85

返回的數(shù)據(jù)為:
 

復(fù)制代碼 代碼如下:

{"ret":1,"start":"123.123.221.0","end":"123.124.158.29","country":"\u4e2d\u56fd","province":"\u5317\u4eac","city":"\u5317\u4eac","district":"","isp":"\u8054\u901a","type":"","desc":""}

其返回的內(nèi)容中已經(jīng)包含了省份+城市+行政區(qū)信息了,這就是我們真實想要的。

下面就來說說如何來抓取這部分IP數(shù)據(jù),要抓取這部分數(shù)據(jù)的主要工作就是枚舉,即將接口中的IP不斷的替換,要替換所有的IP地址肯定不太可能,所以我們縮小下范圍,只窮舉所有中國的IP段??紤]到新浪的IP接口返回的是IP段,所以要窮舉的部分又少了一部分。再考慮啊到IP段的最后一位及256個IP基本上都是在一個地區(qū),所以我們要窮舉的數(shù)據(jù)有少了很多。對于窮舉最重要的是把IP地址換成INT型。

具體國內(nèi)有多少IP地址段,可以到APNIC官方網(wǎng)站去查找或下面的文檔

http://ftp.apnic.net/apnic/dbase/data/country-ipv4.lst

下面就來看看窮舉程序如何寫:

 

import re

 
def ipv3_to_int(s):
  l = [int(i) for i in s.split('.')]
  return (l[0] << 16) | (l[1] << 8) | l[2]

 
def int_to_ipv3(s):
  ip1 = s >> 16 & 0xFF
  ip2 = s >> 8 & 0xFF
  ip3 = s & 0xFF
  return "%d.%d.%d" % (ip1, ip2, ip3)

 
i = open('ChinaIPAddress.csv', 'r')
list = i.readlines()
for iplist in list:
  pattern = re.compile('(\d{1,3}\.\d{1,3}\.\d{1,3})\.\d{1,3}')
  ips = pattern.findall(iplist)
  x = ips[0]
  y = ips[1]
  for ip in range (ipv3_to_int(x),ipv3_to_int(y)):
    ipadress=str(ip)
    #ip_address = int_to_ipv3(ip)
    o = open('ChinaIPAddress.txt','a')
    o.writelines(ipadress)
    o.writelines('\n')
  o.close()
i.close()

當(dāng)上面的不走完成后就可以對新浪IP接口進行抓取了,抓取代碼如下:
 

#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib,urllib2, simplejson, sqlite3, time

 
def ipv3_to_int(s):
  l = [int(i) for i in s.split('.')]
  return (l[0] << 16) | (l[1] << 8) | l[2]

 
def int_to_ipv4(s):
  ip1 = s >> 16 & 0xFF
  ip2 = s >> 8 & 0xFF
  ip3 = s & 0xFF
  return "%d.%d.%d.0" % (ip1, ip2, ip3)

 
def fetch(ipv4, **kwargs):
  kwargs.update({
    'ip': ipv4,
    'format': 'json',
  })
  DATA_BASE = "http://int.dpool.sina.com.cn/iplookup/iplookup.php"
  url = DATA_BASE + '?' + urllib.urlencode(kwargs)
  print url
  fails = 0
  try:
    result = simplejson.load(urllib2.urlopen(url,timeout=20))
  except (urllib2.URLError,IOError):
    fails += 1
    if fails < 10:
      result = fetch(ipv4)
    else:
      sleep_download_time = 60*10
      time.sleep(sleep_download_time)
      result = fetch(ipv4)
  return result

 
def dbcreate():
  c = conn.cursor()
  c.execute('''create table ipdata(
    ip integer primary key,
    ret integer,
    start text,
    end text,
    country text,
    province text,
    city text,
    district text,
    isp text,
    type text,
    desc text
  )''')
  conn.commit()
  c.close()

 
def dbinsert(ip,address):
  c = conn.cursor()
  c.execute('insert into ipdata values(?,?,?,?,?,?,?,?,?,?,?)',(ip,address['ret'],address['start'],address['end'],address['country'],address['province'],address['city'],address['district'],address['isp'],address['type'],address['desc']))
  conn.commit()
  c.close()

 
conn = sqlite3.connect('ipaddress.sqlite3.db')
dbcreate()

 
i = open('ChinaIPAddress.txt','r')
list = [s.strip() for s in i.readlines()]
end = 0
for ip in list:
  ip = int(ip)
  if ip > end :
    ipaddress = int_to_ipv4(ip)
    info = fetch(ipaddress)
    if info['ret'] == -1:
      pass
    else:
      dbinsert(ip,info)
      end = ipv3_to_int(info['end'])
      print ip,end
  else :
    pass
i.close()

到此就能把新浪所有的國內(nèi)IP數(shù)據(jù)給抓取出來,然后在數(shù)據(jù)分析的工程中大派用場。~

相關(guān)文章

最新評論

平凉市| 巴塘县| 靖安县| 德格县| 新平| 亳州市| 陆川县| 沐川县| 元江| 崇明县| 宁海县| 浠水县| 阆中市| 昔阳县| 铅山县| 习水县| 鹤山市| 剑川县| 岱山县| 五台县| 廉江市| 鞍山市| 襄垣县| 大英县| 秭归县| 福海县| 涪陵区| 磐安县| 贡觉县| 沙坪坝区| 石河子市| 凯里市| 张家界市| 鲁山县| 鄂伦春自治旗| 乐至县| 虎林市| 宜州市| 凉城县| 龙口市| 营山县|