python爬蟲抓取站長之家IP庫,僅供練慣用!

來源:互聯網
上載者:User

標籤:爬蟲   python   ip庫   

python爬蟲抓取站長之家IP庫,單線程的,僅供練習,IP庫資料有43億條,如果按此種方法抓取至少得數年,所以謹以此作為練手,新手代碼很糙,請大家見諒。

#!/usr/bin/python#coding=UTF-8import urllib2import reimport osimport csvimport codecsuser_agent = ‘Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)‘headers = { ‘User-Agent‘ : user_agent }def gethtml(url):    user_agent = ‘Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)‘    headers = { ‘User-Agent‘ : user_agent }    request=urllib2.Request(url,headers=headers)    response=urllib2.urlopen(request)    html=response.read()    return htmldef getdate(html):    r=r‘<span class="Whwtdhalf w50\-0">(.*?)</span>\s*</p>‘    rs=re.compile(r,re.S)    iplace=rs.findall(html)    return iplacedef getdataid(html):    r=r‘<span class="Whwtdhalf w15\-0">(\d+?)</span>‘    rs=re.compile(r)    dataid=rs.findall(html)    return dataiddef geturl(ip):    url=‘http://ip.chinaz.com/%s‘%ip    return urlcount=1with open(‘ipku.csv‘,‘a+‘) as csvfile:    csvfile.write(codecs.BOM_UTF8)    spamwriter=csv.writer(csvfile,dialect=‘excel‘)    spamwriter.writerow([‘數字地址‘,‘IP地址‘,‘伺服器位址‘])    for k in xrange(211,256):        for v in xrange(0,256):            for m in xrange(10,256):                for n in xrange(0,256):                    ip=str(k)+‘.‘+str(v)+‘.‘+str(m)+‘.‘+str(n)                    url=geturl(ip)                    html=gethtml(url)                    dataid=getdataid(html)                    iplace=getdate(html)                    for h in dataid:                        print count                        count+=1                        spamwriter.writerow([h,ip,iplace[1]])

列印count是因為讓大家看到進程在運行,沒特殊意義。

本文出自 “全球互聯雲主機Q874247458” 部落格,請務必保留此出處http://gosweet.blog.51cto.com/11759495/1905737

python爬蟲抓取站長之家IP庫,僅供練慣用!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.