Skip to content

Instantly share code, notes, and snippets.

@Dliv3
Created January 11, 2019 03:44
Show Gist options
  • Save Dliv3/2da0f5e20938b9cfabe98b61f6b339ee to your computer and use it in GitHub Desktop.
Save Dliv3/2da0f5e20938b9cfabe98b61f6b339ee to your computer and use it in GitHub Desktop.
# -*- 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