Python爬蟲抓取代理IP並檢驗可用性的執行個體

來源:互聯網
上載者:User
這篇文章主要介紹了關於Python爬蟲抓取代理IP並檢驗可用性的執行個體,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

經常寫爬蟲,難免會遇到ip被目標網站屏蔽的情況,銀次一個ip肯定不夠用,作為節約的程式猿,能不花錢就不花錢,那就自己去找吧,這次就寫了下抓取 西刺代理上的ip,但是這個網站也反爬!!!

至於如何應對,我覺得可以通過增加延時試試,可能是我抓取的太頻繁了,所以被封IP了。

但是,還是可以去IP巴士試試的,條條大路通羅馬嘛,不能弔死在一棵樹上。

不廢話,上代碼。

#!/usr/bin/env python# -*- coding:utf8 -*-import urllib2import timefrom bs4 import BeautifulSoupimport sysreload(sys)sys.setdefaultencoding( "utf-8" )req_header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11', 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', #'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3', 'Accept-Charset':'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 'Accept-Encoding':'en-us', 'Connection':'keep-alive', 'Referer':'http://www.baidu.com/' }req_timeout = 5testUrl = "http://www.baidu.com/"testStr = "wahaha"file1 = open('proxy.txt' , 'w')# url = ""# req = urllib2.Request(url,None,req_header)# jsondatas = urllib2.urlopen(req,None,req_timeout).read()cookies = urllib2.HTTPCookieProcessor()checked_num = 0grasp_num = 0for page in range(1, 160): req = urllib2.Request('http://www.xici.net.co/nn/' + str(page), None, req_header) html_doc = urllib2.urlopen(req, None, req_timeout).read() # html_doc = urllib2.urlopen('http://www.xici.net.co/nn/' + str(page)).read() soup = BeautifulSoup(html_doc) trs = soup.find('table', id='ip_list').find_all('tr') for tr in trs[1:]:  tds = tr.find_all('td')  ip = tds[1].text.strip()  port = tds[2].text.strip()  protocol = tds[5].text.strip()  if protocol == 'HTTP' or protocol == 'HTTPS':   #of.write('%s=%s:%s\n' % (protocol, ip, port))   print '%s=%s:%s' % (protocol, ip, port)   grasp_num +=1   proxyHandler = urllib2.ProxyHandler({"http": r'http://%s:%s' % (ip, port)})   opener = urllib2.build_opener(cookies, proxyHandler)   opener.addheaders = [('User-Agent',         'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36')]   t1 = time.time()   try:    req = opener.open(testUrl, timeout=req_timeout)    result = req.read()    timeused = time.time() - t1    pos = result.find(testStr)    if pos > 1:     file1.write(protocol+"\t"+ip+"\t"+port+"\n")     checked_num+=1     print checked_num, grasp_num    else:     continue   except Exception,e:    continuefile1.close()print checked_num,grasp_num

個人感覺代碼裡沒有太複雜的,就沒有加註釋,相信大家基本可以理解,如有問題也請多批評指正,共同進步!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.