阅读量:85
要使用Python编写一个代理IP爬虫,你需要遵循以下步骤:
- 安装所需库:
首先,确保你已经安装了
requests和fake_useragent库。如果没有,请使用以下命令安装:
pip install requests
pip install fake_useragent
- 导入库: 在你的Python脚本中,导入所需的库:
import requests
from fake_useragent import UserAgent
- 设置代理IP池: 创建一个包含多个代理IP的列表,以便在请求中使用:
proxies = [
{'http': 'http://proxy1:port'},
{'http': 'http://proxy2:port'},
{'http': 'http://proxy3:port'},
# 更多代理IP...
]
- 设置User-Agent:
使用
fake_useragent库生成随机的User-Agent,以避免被目标网站屏蔽:
ua = UserAgent()
- 编写爬虫函数: 创建一个函数,该函数将使用代理IP和User-Agent发送请求并处理响应:
def fetch(url):
proxy = random.choice(proxies)
headers = {'User-Agent': ua.random}
try:
response = requests.get(url, headers=headers, proxies=proxy, timeout=5)
response.raise_for_status()
return response.text
except requests.exceptions.RequestException as e:
print(f"Error fetching {url}: {e}")
return None
- 运行爬虫:
遍历你要抓取的URL列表,并使用
fetch函数获取页面内容:
url_list = [
'https://example.com/page1',
'https://example.com/page2',
# 更多URL...
]
for url in url_list:
content = fetch(url)
if content:
# 处理页面内容,例如保存到文件或解析HTML
with open(f"{url}.html", "w", encoding="utf-8") as f:
f.write(content)
这样,你的代理IP爬虫就可以运行了。请注意,根据目标网站的限制,你可能需要定期更新代理IP池和User-Agent。此外,确保遵循目标网站的robots.txt规则,并遵守相关法律法规。