Python爬蟲使用代理IP的實現(xiàn)
使用爬蟲時,如果目標網(wǎng)站對訪問的速度或次數(shù)要求較高,那么你的 IP 就很容易被封掉,也就意味著在一段時間內(nèi)無法再進行下一步的工作。這時候代理 IP 能夠給我們帶來很大的便利,不管網(wǎng)站怎么封,只要能找到一個新的代理 IP 就可以繼續(xù)進行下一步的研究。
目前很多網(wǎng)站都提供了一些免費的代理 IP 供我們使用,當然付費的會更好用一點。本文除了展示怎樣使用代理 IP,也正好體驗一下前面文章中搭建的代理 IP 池,不知道的可以點擊這里:Python搭建代理IP池(一)- 獲取 IP。只要訪問代理池提供的接口就可以獲取到代理 IP 了,接下來就看怎樣使用吧!
測試的網(wǎng)址是:http://httpbin.org/get,訪問該站點可以得到請求的一些相關信息,其中 origin 字段就是客戶端的 IP,根據(jù)它來判斷代理是否設置成功,也就是是否成功偽裝了IP
獲取 IP
代理池使用 Flask 提供了獲取的接口:http://localhost:5555/random
只要訪問這個接口再返回內(nèi)容就可以拿到 IP 了
Urllib
先看一下 Urllib 的代理設置方法:
from urllib.error import URLError
import urllib.request
from urllib.request import ProxyHandler, build_opener
# 獲取IP
ip_response = urllib.request.urlopen("http://localhost:5555/random")
ip = ip_response.read().decode('utf-8')
proxy_handler = ProxyHandler({
'http': 'http://' + ip,
'https': 'https://' + ip
})
opener = build_opener(proxy_handler)
try:
response = opener.open('http://httpbin.org/get')
print(response.read().decode('utf-8'))
except URLError as e:
print(e.reason)
運行結果:
{
"args": {},
"headers": {
"Accept-Encoding": "identity",
"Host": "httpbin.org",
"User-Agent": "Python-urllib/3.7"
},
"origin": "108.61.201.231, 108.61.201.231",
"url": "https://httpbin.org/get"
}
Urllib 使用 ProxyHandler 設置代理,參數(shù)是字典類型,鍵名為協(xié)議類型,鍵值是代理,代理前面需要加上協(xié)議,即 http 或 https,當請求的鏈接是 http 協(xié)議的時候,它會調(diào)用 http 代理,當請求的鏈接是 https 協(xié)議的時候,它會調(diào)用https代理,所以此處生效的代理是:http://108.61.201.231 和 https://108.61.201.231
ProxyHandler 對象創(chuàng)建之后,再利用 build_opener() 方法傳入該對象來創(chuàng)建一個 Opener,這樣就相當于此 Opener 已經(jīng)設置好代理了,直接調(diào)用它的 open() 方法即可使用此代理訪問鏈接
Requests
Requests 的代理設置只需要傳入 proxies 參數(shù):
import requests
# 獲取IP
ip_response = requests.get("http://localhost:5555/random")
ip = ip_response.text
proxies = {
'http': 'http://' + ip,
'https': 'https://' + ip,
}
try:
response = requests.get('http://httpbin.org/get', proxies=proxies)
print(response.text)
except requests.exceptions.ConnectionError as e:
print('Error', e.args)
運行結果:
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.21.0"
},
"origin": "47.90.28.54, 47.90.28.54",
"url": "https://httpbin.org/get"
}
Requests 只需要構造代理字典然后通過 proxies 參數(shù)即可設置代理,比較簡單
Selenium
import requests
from selenium import webdriver
import time
# 借助requests庫獲取IP
ip_response = requests.get("http://localhost:5555/random")
ip = ip_response.text
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=http://' + ip)
browser = webdriver.Chrome(chrome_options=chrome_options)
browser.get('http://httpbin.org/get')
time.sleep(5)
運行結果:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python使用urllib模塊的urlopen超時問題解決方法
這篇文章主要介紹了Python使用urllib模塊的urlopen超時問題解決方法,本文使用socket模塊中的setdefaulttimeout函數(shù)解決了超時問題,需要的朋友可以參考下2014-11-11
Python中用psycopg2模塊操作PostgreSQL方法
python可以操作多種數(shù)據(jù)庫,本篇文章給大家介紹了用psycopg2模塊操作PostgreSQL方法,一起來學習下。2017-11-11
NumPy 數(shù)學函數(shù)及代數(shù)運算的實現(xiàn)代碼
這篇文章主要介紹了NumPy 數(shù)學函數(shù)及代數(shù)運算的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
python連接access數(shù)據(jù)庫兩種方式總結
這篇文章主要介紹了python連接access數(shù)據(jù)庫兩種方式的相關資料,SQLAlchemy使用access方言進行連接,而pyodbc則通過pyodbc模塊實現(xiàn)連接,文章還提供了連接代碼示例,需要的朋友可以參考下2025-02-02
Python實現(xiàn)對二維碼數(shù)據(jù)進行壓縮
當前二維碼的應用越來越廣泛,包括疫情時期的健康碼也是應用二維碼的典型案例。本文的目標很明確,就是使用python,實現(xiàn)一張二維碼顯示更多信息,代碼簡單實用,感興趣的可以了解一下2023-02-02

