国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

python爬蟲利用代理池更換IP的方法步驟

瀏覽:205日期:2022-06-27 13:34:41
0. 前言

周日在爬一個國外網站的時候,發現用協程并發請求,并且請求次數太快的時候,會出現對方把我的服務器IP封掉的情況。于是網上找了一下開源的python代理池,這里選擇的是star數比較多的proxy_pool

1. 安裝環境

# 安裝python虛擬環境, python環境最好為python3.6,再往上的話,安裝依賴時會報錯sudo apt updatesudo apt install python3.6pip3 install virtualenvvirtualenv venv --python=python3.6source venv/bin/activate# 安裝redissudo apt install redis-server# 啟動redis serverredis-server 2. 安裝依賴

git clone https://github.com/jhao104/proxy_pool.gitcd proxy_poolpip install -r requirements.txt3. 修改配置文件

# 修改setting.py # 配置API服務HOST = '0.0.0.0' # IPPORT = 5010 # 監聽端口# 配置數據庫# 以下為三個示例,根據redis的配置,選擇其中一種即可# 一般啟動redis時如果沒有配置文件,那么選擇第一種即可# 1. Redis IP: 127.0.0.1 Port: 6379DB_CONN = ’redis://@127.0.0.1:6379’# 2. Redis IP: 127.0.0.1 Port: 6379 Password: 123456DB_CONN = ’redis://:123456@127.0.0.1:6379’# 3. Redis IP: 127.0.0.1 Port: 6379 Password: 123456 DB: 15DB_CONN = ’redis://:123456@127.0.0.1:6379/15’ # 配置 ProxyFetcherPROXY_FETCHER = [ 'freeProxy01', # 這里是啟用的代理抓取方法名,所有fetch方法位于fetcher/proxyFetcher.py 'freeProxy02', # ....]4. 啟動

# 可以用tmux開三個窗口# 啟動調度程序python proxyPool.py schedule# 啟動webApi服務python proxyPool.py server5. 測試

import requestsdef get_proxy(): return requests.get('http://127.0.0.1:5010/get/').json()def delete_proxy(proxy): requests.get('http://127.0.0.1:5010/delete/?proxy={}'.format(proxy))# your spider codedef getHtml(): # .... retry_count = 5 proxy = get_proxy().get('proxy') while retry_count > 0: try: html = requests.get(’http://www.example.com’, proxies={'http': 'http://{}'.format(proxy)}) # 使用代理訪問 return html except Exception: retry_count -= 1 # 刪除代理池中代理 delete_proxy(proxy) return None

python爬蟲利用代理池更換IP的方法步驟

更多的用法和文檔請參考:document 和 https://github.com/jhao104/proxy_pool

到此這篇關于python爬蟲利用代理池更換IP的方法步驟的文章就介紹到這了,更多相關python 代理池更換IP內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 铜梁县| 大足县| 崇文区| 林芝县| 油尖旺区| 珲春市| 台北市| 西华县| 通山县| 肃宁县| 霍城县| 津市市| 德阳市| 庐江县| 常州市| 宝兴县| 安泽县| 永年县| 东阿县| 铜鼓县| 屯留县| 新昌县| 邢台市| 高邮市| 盐源县| 达州市| 积石山| 万山特区| 德惠市| 黑龙江省| 永修县| 靖安县| 巴彦淖尔市| 新乐市| 丹寨县| 灵璧县| 冕宁县| 濮阳县| 楚雄市| 汾阳市| 繁峙县|