Created
January 11, 2019 03:44
-
-
Save Dliv3/2da0f5e20938b9cfabe98b61f6b339ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# https://github.com/jhao104/proxy_pool | |
import requests | |
import time | |
def get_proxy(): | |
return requests.get("http://127.0.0.1:5010/get/").content | |
def delete_proxy(proxy): | |
requests.get("http://127.0.0.1:5010/delete/?proxy={}".format(proxy)) | |
# your spider code | |
def getHtml(url): | |
# .... | |
retry_count = 5 | |
proxy = get_proxy() | |
while retry_count > 0: | |
try: | |
html = requests.get(url, proxies={"http": "http://{}".format(proxy)}) | |
# 使用代理访问 | |
return html | |
except Exception: | |
retry_count -= 1 | |
# 出错5次, 删除代理池中代理 | |
delete_proxy(proxy) | |
return None | |
while True: | |
print getHtml('http://test.com') | |
time.sleep(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment