Python爬虫——建立IP代理池
在使用Python爬虫时,经常遇见具有反爬机制的网站。我们可以通过伪装headers来爬取,但是网站还是可以获取你的ip,从而禁掉你的ip来阻止爬取信息。 在request方法中,我们可以通过proxies参数来伪装我们的ip,一些网站上有免费的ip代理网站,可以通过爬取这些ip,经检测后建立ip代理池。 ip代理网站: ( https://www.xicidaili.com/nt/ ) ( https://www.kuaidaili.com/free/intr/ ) 推荐一种常用的伪装头方法 from fake_useragent import UserAgent ua = UserAgent ( ) headers = { 'User-Agent' : ua . random } 接下来进入正题 爬取ip(IPPool.py) import requests from lxml import etree from fake_useragent import UserAgent #伪装 ua = UserAgent ( ) headers = { 'User-Agent' : ua . random } def get_ip ( ) : ip_list = [ ] #路径 url = 'https://www.xicidaili.com/nt/' #ip是有时效的,只爬取第一页