如何建立爬蟲代理ip池
犀牛小牛發表於2019-04-25
一、為什麼需要建立爬蟲代理ip池
在眾多的網站防爬措施中,有一種是根據ip的訪問頻率進行限制的,在某段時間內,當某個ip的訪問量達到一定的閥值時, 該ip會被拉黑、在一段時間內被禁止訪問這種時候,可以透過降低爬蟲的頻率,或者更改ip來應對。後者就需要有一個可 用的代理ip池,以供爬蟲工作時切換。這個就是更換IP軟體。
二、如何建立一個爬蟲代理ip池
思路:
1、找到一個免費的ip代理網站(如:西刺代理)
2、爬取ip(常規爬取requests+BeautifulSoup)
3、驗證ip有效性(攜帶爬取到的ip,去訪問指定的url,看返回的狀態碼是不是200)
4、記錄ip (寫到文件)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests,threading,datetime
from bs4 import BeautifulSoup
import random
1、抓取西刺代理網站的代理ip
2、並根據指定的目標url,對抓取到ip的有效性進行驗證
3、最後存到指定的path
文件處理
def write(path,text): with open(path,'a', encoding='utf-8') as f: f.writelines(text) f.write('\n')清空文件
def truncatefile(path): with open(path, 'w', encoding='utf-8') as f: f.truncate()讀取文件
def read(path): with open(path, 'r', encoding='utf-8') as f: txt = [] for s in f.readlines(): txt.append(s.strip()) return txt計算時間差,格式: 時分秒
def gettimediff(start,end): seconds = (end - start).seconds m, s = divmod(seconds, 60) h, m = divmod(m, 60) diff = ("%02d:%02d:%02d" % (h, m, s)) return diff返回一個隨機的請求頭 headers
def getheaders():user_agent_list = [ \ "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1" \ "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11", \ "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6", \ "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6", \ "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1", \ "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5", \ "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5", \ "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", \ "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", \ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", \ "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3", \ "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3", \ "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", \ "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", \ "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", \ "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3", \ "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24", \ "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24" ] UserAgent=random.choice(user_agent_list) headers = {'User-Agent': UserAgent} return headers檢查ip是否可用
def checkip(targeturl,ip): headers =getheaders() # 定製請求頭 proxies = {"http": "} # 代理ip try: response=requests.get(url=targeturl,proxies=proxies,headers=headers,timeout=5).status_code if response == 200 : return True else: return False except: return False獲取代理方法 免費代理 XiciDaili
def findip(type,pagenum,targeturl,path): # ip型別,頁碼,目標url,存放ip的路徑 list={'1': '國內普通代理 '2': '國內高匿代理 '3': '國內https代理 '4': '} # xicidaili國外http代理 url=list[str(type)]+str(pagenum) # 配置url headers = getheaders() # 定製請求頭 html=requests.get(url=url,headers=headers,timeout = 5).text soup=BeautifulSoup(html,'lxml') all=soup.find_all('tr',class_='odd') for i in all: t=i.find_all('td') ip=t[1].text+':'+t[2].text is_avail = checkip(targeturl,ip) if is_avail == True: write(path=path,text=ip) print(ip)多執行緒抓取ip入口
def getip(targeturl,path): truncatefile(path) # 爬取前清空文件 start = datetime.datetime.now() # 開始時間 threads=[] for type in range(4): # 四種型別ip,每種型別取前三頁,共12條執行緒 for pagenum in range(3): t=threading.Thread(target=findip,args=(type+1,pagenum+1,targeturl,path)) threads.append(t) print('開始爬取代理ip') for s in threads: # 開啟多執行緒爬取 s.start() for e in threads: # 等待所有執行緒結束 e.join() print('爬取完成') end = datetime.datetime.now() # 結束時間 diff = gettimediff(start, end) # 計算耗時 ips = read(path) # 讀取爬到的ip數量 print('一共爬取代理ip: %s 個,共耗時: %s \n' % (len(ips), diff))啟動
if __name__ == '__main__': path = 'ip.txt' # 存放爬取ip的文件path targeturl = 'http://www.cnblogs.com/TurboWay/' # 驗證ip有效性的指定url getip(targeturl,path) http網頁線上代理
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31555707/viewspace-2642531/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 如何建立爬蟲IP池?2022-06-07爬蟲
- 爬蟲如何使用ip代理池2021-09-11爬蟲
- 手把手教你爬蟲代理ip池的建立2021-09-11爬蟲
- 如何用海外HTTP代理設定python爬蟲代理ip池?2022-08-30HTTPPython爬蟲
- 代理ip池對爬蟲有多重要2021-09-11爬蟲
- Python 爬蟲IP代理池的實現2018-12-17Python爬蟲
- 代理IP如何突破反爬蟲?2021-09-11爬蟲
- scrapy爬蟲代理池2018-08-28爬蟲
- 代理ip池對爬蟲有什麼好處2021-09-11爬蟲
- 快速構建Python爬蟲IP代理池服務2018-03-01Python爬蟲
- 構建一個給爬蟲使用的代理IP池2017-11-23爬蟲
- python 爬蟲 代理池2019-03-09Python爬蟲
- Python爬蟲代理池2016-12-09Python爬蟲
- 爬蟲代理IP產品如何選擇2021-07-14爬蟲
- 如何用http代理的ip池繞過網站反爬蟲機制?2023-05-04HTTP網站爬蟲
- 爬蟲採集自建代理ip池的三大優勢2022-05-18爬蟲
- 爬蟲之代理池維護2018-08-18爬蟲
- 爬蟲使用代理防封IP2019-04-17爬蟲
- 爬蟲代理怎麼選ip2021-09-11爬蟲
- 爬蟲代理IP的使用技巧2022-07-12爬蟲
- 如何利用代理ip提高爬蟲的工作效率2021-09-11爬蟲
- 實用爬蟲-02-爬蟲真正使用代理 ip2018-09-08爬蟲
- 爬蟲代理IP有哪些好處?2021-09-11爬蟲
- 爬蟲被封怎麼辦?用Node構建一個私人IP代理池2019-04-27爬蟲
- 爬蟲ip代理池搭建前需解決的問題及搭建思路2018-12-20爬蟲
- 【PhpSelenium】3.定時爬蟲 + 多工爬蟲 + 代理池2019-12-17PHP爬蟲
- [PhpSelenium] 3.定時爬蟲 + 多工爬蟲 + 代理池2019-12-17PHP爬蟲
- 用Python爬蟲抓取代理IP2019-04-17Python爬蟲
- 爬蟲中代理IP的常見方案2021-09-11爬蟲
- 爬蟲需要代理IP的基本要求2021-09-11爬蟲
- 網路爬蟲怎麼使用ip代理2021-09-11爬蟲
- Python爬蟲技巧---設定代理IP2018-07-12Python爬蟲
- 爬蟲代理IP的三大作用2022-05-21爬蟲
- python爬蟲從ip池獲取隨機IP2021-09-11Python爬蟲隨機
- 使用芝麻代理,爬蟲ip經常被封,該如何解決?2023-01-13爬蟲
- python爬蟲實戰:爬取西刺代理的代理ip(二)2019-02-16Python爬蟲
- 爬蟲為什麼要用IP代理更換IP地址2021-09-11爬蟲
- python爬蟲之反爬蟲(隨機user-agent,獲取代理ip,檢測代理ip可用性)2019-01-03Python爬蟲隨機