Python爬蟲實現(xiàn)搭建代理ip池
前言
在使用爬蟲的時候,很多網(wǎng)站都有一定的反爬措施,甚至在爬取大量的數(shù)據(jù)或者頻繁地訪問該網(wǎng)站多次時還可能面臨ip被禁,所以這個時候我們通常就可以找一些代理ip來繼續(xù)爬蟲測試。
下面就開始來簡單地介紹一下爬取免費的代理ip來搭建自己的代理ip池:本次爬取免費ip代理的網(wǎng)址:http://www.ip3366.net/free/
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、User-Agent
在發(fā)送請求的時候,通常都會做一個簡單的反爬。這時可以用fake_useragent模塊來設(shè)置一個請求頭,用來進(jìn)行偽裝成瀏覽器,下面兩種方法都可以。
from fake_useragent import UserAgent
headers = {
# 'User-Agent': UserAgent().random #常見瀏覽器的請求頭偽裝(如:火狐,谷歌)
'User-Agent': UserAgent().Chrome #谷歌瀏覽器
}
二、發(fā)送請求
response = requests.get(url='http://www.ip3366.net/free/', headers=request_header())
# text = response.text.encode('ISO-8859-1')
# print(text.decode('gbk'))三、解析數(shù)據(jù)
我們只需要解析出ip、port即可。


使用xpath解析(個人很喜歡用)(當(dāng)然還有很多的解析方法,如:正則,css選擇器,BeautifulSoup等等)。
#使用xpath解析,提取出數(shù)據(jù)ip,端口
html = etree.HTML(response.text)
tr_list = html.xpath('/html/body/div[2]/div/div[2]/table/tbody/tr')
for td in tr_list:
ip_ = td.xpath('./td[1]/text()')[0] #ip
port_ = td.xpath('./td[2]/text()')[0] #端口
proxy = ip_ + ':' + port_ #115.218.5.5:9000四、構(gòu)建ip代理池,檢測ip是否可用
#構(gòu)建代理ip
proxy = ip + ':' + port
proxies = {
"http": "http://" + proxy,
"https": "http://" + proxy,
# "http": proxy,
# "https": proxy,
}
try:
response = requests.get(url='https://www.baidu.com/',headers=request_header(),proxies=proxies,timeout=1) #設(shè)置timeout,使響應(yīng)等待1s
response.close()
if response.status_code == 200:
print(proxy, '\033[31m可用\033[0m')
else:
print(proxy, '不可用')
except:
print(proxy,'請求異常')五、完整代碼
import requests #導(dǎo)入模塊
from lxml import etree
from fake_useragent import UserAgent
#簡單的反爬,設(shè)置一個請求頭來偽裝成瀏覽器
def request_header():
headers = {
# 'User-Agent': UserAgent().random #常見瀏覽器的請求頭偽裝(如:火狐,谷歌)
'User-Agent': UserAgent().Chrome #谷歌瀏覽器
}
return headers
'''
創(chuàng)建兩個列表用來存放代理ip
'''
all_ip_list = [] #用于存放從網(wǎng)站上抓取到的ip
usable_ip_list = [] #用于存放通過檢測ip后是否可以使用
#發(fā)送請求,獲得響應(yīng)
def send_request():
#爬取7頁,可自行修改
for i in range(1,8):
print(f'正在抓取第{i}頁……')
response = requests.get(url=f'http://www.ip3366.net/free/?page={i}', headers=request_header())
text = response.text.encode('ISO-8859-1')
# print(text.decode('gbk'))
#使用xpath解析,提取出數(shù)據(jù)ip,端口
html = etree.HTML(text)
tr_list = html.xpath('/html/body/div[2]/div/div[2]/table/tbody/tr')
for td in tr_list:
ip_ = td.xpath('./td[1]/text()')[0] #ip
port_ = td.xpath('./td[2]/text()')[0] #端口
proxy = ip_ + ':' + port_ #115.218.5.5:9000
all_ip_list.append(proxy)
test_ip(proxy) #開始檢測獲取到的ip是否可以使用
print('抓取完成!')
print(f'抓取到的ip個數(shù)為:{len(all_ip_list)}')
print(f'可以使用的ip個數(shù)為:{len(usable_ip_list)}')
print('分別有:\n', usable_ip_list)
#檢測ip是否可以使用
def test_ip(proxy):
#構(gòu)建代理ip
proxies = {
"http": "http://" + proxy,
"https": "http://" + proxy,
# "http": proxy,
# "https": proxy,
}
try:
response = requests.get(url='https://www.baidu.com/',headers=request_header(),proxies=proxies,timeout=1) #設(shè)置timeout,使響應(yīng)等待1s
response.close()
if response.status_code == 200:
usable_ip_list.append(proxy)
print(proxy, '\033[31m可用\033[0m')
else:
print(proxy, '不可用')
except:
print(proxy,'請求異常')
if __name__ == '__main__':
send_request()總結(jié)
到此這篇關(guān)于Python爬蟲實現(xiàn)搭建代理ip池的文章就介紹到這了,更多相關(guān)Python代理ip池內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python語言中pandas字符串分割str.split()函數(shù)
分列在我們?nèi)粘9ぷ髦薪?jīng)常用到,從各種系統(tǒng)中導(dǎo)出的什么訂單號、名稱、日期很多都是復(fù)合組成的,這些列在匹配、合并時沒有辦法使用,我們經(jīng)常需要將她們分開,下面這篇文章主要給大家介紹了關(guān)于python語言中pandas字符串分割str.split()函數(shù)的相關(guān)資料,需要的朋友可以參考下2022-08-08
Flask如何獲取用戶的ip,查詢用戶的登錄次數(shù),并且封ip
這篇文章主要介紹了Flask如何獲取用戶的ip,查詢用戶的登錄次數(shù),并且封ip問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
一步步教你用python連接oracle數(shù)據(jù)庫
oracle作為最強大的數(shù)據(jù)庫,Python也提供了足夠的支持。不過與其他數(shù)據(jù)庫略有不同,下面這篇文章主要給大家介紹了關(guān)于如何使用python連接oracle數(shù)據(jù)庫的相關(guān)資料,需要的朋友可以參考下2023-04-04
使用Flask創(chuàng)建簡單的圖片上傳站點的流程步驟
在網(wǎng)絡(luò)應(yīng)用程序中,實現(xiàn)圖片上傳功能是一項常見的需求,Flask框架提供了簡單而靈活的工具,使得構(gòu)建這樣的功能變得相對簡單,本文將介紹如何使用Flask框架創(chuàng)建一個簡單的圖片上傳站點,以及其中涉及的關(guān)鍵技術(shù)和步驟,需要的朋友可以參考下2024-05-05
Python使用BeautifulSoup爬取網(wǎng)頁數(shù)據(jù)的操作步驟
在網(wǎng)絡(luò)時代,數(shù)據(jù)是最寶貴的資源之一,而爬蟲技術(shù)就是一種獲取數(shù)據(jù)的重要手段,Python 作為一門高效、易學(xué)、易用的編程語言,自然成為了爬蟲技術(shù)的首選語言之一,本文將介紹如何使用 BeautifulSoup 爬取網(wǎng)頁數(shù)據(jù),并提供詳細(xì)的代碼和注釋,幫助讀者快速上手2023-11-11

