1: 关于代理ip的安装,见同桌
2: 导入可能用到的模块
import requests from requests.exceptions import ConnectionError
3: 定义获得代理ip的函数
def get_proxy(): try: response = requests.get("http://127.0.0.1:5010/get/") if response.status_code == 200: return response.text return None except Exception as e: print('获取代理异常:',e) return None
4:定义函数使用代理ip
def get_page_list(url): global connection_count proxies = get_proxy() print('列表页代理:', proxies) # 请求url,获取源码 if proxies != None: proxies = { 'http': 'http://'+proxies } try: response = requests.get(url, allow_redirects=False, headers=headers, proxies=proxies) if response.status_code == 200: print('列表页{}请求成功',url) return response.text print('状态码:',response.status_code) if response.status_code == 302: # 切换代理,递归调用当前函数。 get_page_list(url) except ConnectionError as e: print('连接对方主机{}失败: {}',url,e) connection_count += 1 if connection_count == 3: return None # 增加连接次数的判断 get_page_list(url)
5:这就是代理ip的安装和使用