首发于:https://mp.weixin.qq.com/s/O0...如何使用ip既然找到了免费代理ip,我们就想使用it,如何使用为了使用,不能一一复制,那岂不是太傻了?我们使用爬虫技术抓取这些免费的代理IP,并将其存入数据库。后面我们使用的时候,直接用程序提取数据库中的IP就可以了。代理ip,不就可以了吗?思路还是很简单明了的。下面开始抓取各个网站的代理ip...抓取快速代理准备网站:https://www.kuaidaili.com/free/system:windows浏览器:Google语言:python版本:3.x数据库:MongoDB分析URL先打开URL看看:https://www.kuaidadaili.com/free/我们再点开第二页看看URL变了,比较有可能是get请求。我们按F12,打开开发者模式,然后点击第一页,可以看到下图。根据图中的步骤,我们可以发现这是我们要实现导入请求的URL代码fromlxmlimportetreeimportpymongoimporttimeclassget_ip:def__init__(self):_mongo=pymongo.MongoClient(host="127.0.0.1",port=27017)db=_mongo.IPSself.ip_table=db["ip_table"]defget_html(self,page):headers={'Connection':'keep-alive','sec-ch-ua':'"GoogleChrome";v="87","Not;ABrand";v="99","Chromium";v="87"','sec-ch-ua-mobile':'?0','Upgrade-Insecure-Requests':'1','User-Agent':'Mozilla/5.0(WindowsNT10.0;64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/87.0.4280.88Safari/537.36','Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9','Sec-Fetch-Site':'same-origin','Sec-Fetch-Mode':'navigate','Sec-Fetch-User':'?1','Sec-Fetch-Dest':'document','Referer':f'https://www.kuaidaili.com/free/inha/{page}/','Accept-Language':'zh-CN,zh-TW;q=0.9,zh;q=0.8,en;q=0.7',}response=response=requests.get(f'https://www.kuaidaili.com/free/inha/{page}/',headers=headers)html=etree.HTML(response.text)trs=html.xpath('//div[@id="list"]//table/tbody/tr')print(trs)print(response)fortrintrs:ip=tr.xpath('td[@data-title="IP"]/text()')[0]port=tr.xpath('td[@data-title="PORT"]/text()')[0]print(f"{ip}:{port}")_find=self.ip_table.find_one({"ip":ip,"post":port})#去重ifnot_find:print("ip不存在")self.ip_table.insert_one({"ip":ip,"post":port})else:print("ipalreadyexists")g=get_ip()foriinrange(1,9999):g.get_html(i)time.sleep(3)更多内容关注我
