首页 / 爬虫 / 【python3】建立爬虫代理ip池
【python3】建立爬虫代理ip池
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【python3】建立爬虫代理ip池,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2147字,纯文字阅读大概需要4分钟。
内容图文
【python3】建立爬虫代理ip池
起因
一般来说,我们在爬取其他网站的数据的时候,会遇到ip被限制的情况,这时候就需要代理ip池进行处理了。
代码块
我们这里使用了西刺代理池进行模拟,直接上代码:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests, threading, datetime
from bs4 import BeautifulSoup
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# ip清洗
def checkip(targeturl,ip):
proxies = {"http": "http://"+ip, "https": "http://"+ip} # 代理ip
try:
response=requests.get(url=targeturl, verify=False, proxies=proxies,headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'},timeout=5).status_code
if response == 200:
return True
else:
return False
except:
return False
# 免费代理 XiciDaili
def findip(type,pagenum,targeturl): # ip类型,页码,目标url,存放ip的路径
list={'1': 'http://www.xicidaili.com/nt/', # xicidaili国内普通代理
'2': 'http://www.xicidaili.com/nn/', # xicidaili国内高匿代理
'3': 'http://www.xicidaili.com/wn/', # xicidaili国内https代理
'4': 'http://www.xicidaili.com/wt/'} # xicidaili国外http代理
url = list[str(type)]+str(pagenum) # 配置url
html=requests.get(url=url, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'},timeout = 5).text
soup = BeautifulSoup(html, 'lxml')
all = soup.find_all('tr', class_='odd')
for i in all:
t = i.find_all('td')
ip = t[1].text+':'+t[2].text
is_avail = checkip(targeturl, ip)
if is_avail == True:
# write(path=path,text=ip)
print(ip)
def getip(targeturl):
# truncatefile(path) # 爬取前清空文档
start = datetime.datetime.now() # 开始时间
threads=[]
for type in range(4):
for pagenum in range(10):
t=threading.Thread(target=findip,args=(type+1,pagenum+1,targeturl))
threads.append(t)
print('开始爬取代理ip')
for s in threads: # 开启多线程爬取
s.start()
for e in threads: # 等待所有线程结束
e.join()
print('爬取完成')
if __name__ == '__main__':
targeturl = 'https://blog.csdn.net/u011928550/article/details/60154191' # 验证ip有效性的指定url
getip(targeturl)
运行结果:
你可以将targeturl修改为你需要访问的地址,就行了。
内容总结
以上是互联网集市为您收集整理的【python3】建立爬虫代理ip池全部内容,希望文章能够帮你解决【python3】建立爬虫代理ip池所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。