
爬虫开发中,代理IP是解决IP封禁、高频访问限流、地域访问限制的核心手段。很多新手在实操时,常会出现代理配置无效、连接超时、隐私泄露、异步请求代理不生效等问题,本质是不同爬虫库的代理配置规则、参数格式和适配场景存在差异。
下面整理Python三大主流爬虫库的代理IP完整设置方案,覆盖普通代理、私密代理、随机代理、HTTPS适配、账号密码认证等全场景,代码可直接复用,搭配实操避坑要点,零基础也能轻松上手。
所有Python爬虫库的代理配置,核心都遵循统一的IP格式规范,提前掌握可规避80%的配置错误:
协议://IP:端口,示例:http://112.xx.xx.xx:8080协议://账号:密码@IP:端口,示例:http://user123:pass456@112.xx.xx.xx:8080核心原则:访问HTTP网站用HTTP代理,访问HTTPS网站建议配置HTTPS代理,双协议同时配置可实现全场景适配。
Requests是最常用的同步爬虫库,代理配置简单灵活,支持单次请求独立代理、全局会话代理两种模式,适配绝大多数简易爬虫场景。
适合需要每次请求随机更换代理IP的场景,无需全局配置,单次请求生效。
import requests
# www.zdaye.com
#定义代理字典(同时配置http和https,适配所有网站)
proxies = {
"http": "http://账号:密码@代理IP:端口",
"https": "http://账号:密码@代理IP:端口"
}
# 发起请求,传入proxies参数
url = "https://www.66daili.com/ip-location/" # 测试IP接口,可返回当前请求IP
response = requests.get(url, proxies=proxies, timeout=10)
print(response.text)适合需要保持会话、多次请求复用同一个代理的场景,避免重复配置,适配需要cookie留存的爬虫。
import requests
# www.zdaye.com
# 创建会话对象,全局绑定代理
session = requests.Session()
session.proxies = {
"http": "http://账号:密码@代理IP:端口",
"https": "http://账号:密码@代理IP:端口"
}
# 多次请求自动复用代理
url = "https://www.66daili.com/ip-location/"
res1 = session.get(url)
res2 = session.get(url)
print(res1.text)Aiohttp是Python异步爬虫核心库,高并发场景下效率远超同步爬虫,其代理配置和Requests不通用,不支持proxies字典,需单独配置connector参数。
import aiohttp
import asyncio
async def proxy_spider():
# 代理地址(私密代理格式)
proxy = "http://账号:密码@代理IP:端口"
# 创建连接器,绑定代理
connector = aiohttp.TCPConnector(ssl=False) # 关闭SSL校验,避免HTTPS报错
async with aiohttp.ClientSession(connector=connector) as session:
async with session.get("https://httpbin.org/ip", proxy=proxy, timeout=aiohttp.ClientTimeout(total=10)) as res:
print(await res.text())
# 运行异步任务
asyncio.run(proxy_spider())异步爬虫高频请求易触发封禁,可配置代理池随机切换IP,提升稳定性。
import aiohttp
import asyncio
import random
# 代理池列表
proxy_list = [
"http://账号:密码@IP1:端口",
"http://账号:密码@IP2:端口",
"http://账号:密码@IP3:端口"
]
async def batch_spider():
connector = aiohttp.TCPConnector(ssl=False)
async with aiohttp.ClientSession(connector=connector) as session:
# 随机选取代理
proxy = random.choice(proxy_list)
res = await session.get("https://httpbin.org/ip", proxy=proxy)
print(await res.text())
asyncio.run(batch_spider())Selenium用于模拟真实浏览器爬虫,适配JS动态渲染页面,其代理配置是浏览器层级,而非请求层级,配置后浏览器所有网络请求都会走代理,分为Chrome、Firefox两大主流浏览器适配方案。
支持普通代理和带账号密码的私密代理,适配新版Chrome驱动。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# 配置Chrome参数
chrome_options = Options()
# 设置代理IP(无认证普通代理)
proxy_ip = "代理IP:端口"
chrome_options.add_argument(f"--proxy-server=http://{proxy_ip}")
# 忽略SSL报错
chrome_options.add_argument("--ignore-certificate-errors")
# 启动浏览器
driver = webdriver.Chrome(options=chrome_options)
# 测试访问
driver.get("https://httpbin.org/ip")
print(driver.page_source)
driver.quit()Chrome原生参数不支持直接携带账号密码,需通过插件脚本实现认证,适配绝大多数付费私密代理。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import zipfile
import os
# 代理信息
proxy_host = "代理IP"
proxy_port = "端口"
proxy_user = "账号"
proxy_pwd = "密码"
# 生成代理认证插件
def create_proxy_plugin():
manifest = """
{
"version": "1.0",
"manifest_version": 2,
"name": "Proxy Auth",
"permissions": ["proxy", "tabs", "<all_urls>"],
"background": {"scripts": ["background.js"]}
}
"""
background = f"""
chrome.proxy.settings.set({{value:{{mode:"fixed_servers",rules:{{singleProxy:{{scheme:"http",host:"{proxy_host}",port:{proxy_port}}}}}},scope:"regular"}});
chrome.webRequest.onAuthRequired.addListener(
function(details){{return {{authCredentials:{{username:"{proxy_user}",password:"{proxy_pwd}"}}}}},
{{urls:["<all_urls>"]}},
["blocking"]
);
"""
# 写入插件文件
with open("manifest.json", "w") as f:
f.write(manifest)
with open("background.js", "w") as f:
f.write(background)
# 打包插件
with zipfile.ZipFile("proxy_plugin.zip", "w") as zp:
zp.write("manifest.json")
zp.write("background.js")
return os.path.abspath("proxy_plugin.zip")
# 加载插件启动浏览器
chrome_options = Options()
plugin_path = create_proxy_plugin()
chrome_options.add_extension(plugin_path)
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://httpbin.org/ip")
driver.quit()原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。