首页 > 分享 > Python3网络爬虫(三)

Python3网络爬虫(三)

最新推荐文章于 2024-07-18 20:32:06 发布

凡凡不知所错 于 2019-01-07 11:31:54 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

对于一些网站,一开始能请求,但是时间久了,网站有可能会封ip,Requests库对此的解决办法:

import requests proxies = { 'http': 'http://10.10.1.10:3128', 'https': 'http://10.10.1.10:1080', } requests.get('https://www.taobao.com', proxies=proxies) 1234567

如果代理需要HTTP Basic Auth:

import requests proxies = { 'https': 'http://user:password@10.10.1.10:3128/', } requests.get('https://www.taobao.com', proxies=proxies) 12345

!!!!这里如果我们想用各种不同的ip来访问网站呢???
因为对单一IP,很多网站会设置访问间隔。
解决办法,先去免费IP网站爬取所有的IP地址,然后使用这些IP爬取目标网站:

import requests from bs4 import BeautifulSoup headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0' } r = requests.get("https://www.xicidaili.com/nt/", headers=headers) soup = BeautifulSoup(r.text, 'lxml') ips = soup.findAll('tr') proxy_list = [] for x in range(1, len(ips)): ip = ips[x] tds = ip.findAll("td") ip_temp = 'http://'+tds[1].contents[0]+":"+tds[2].contents[0] proxy_list.append(ip_temp) # 上面已经获取了IP,下面是爬取目标网站 run_times = 100000 for i in range(run_times): for item in proxy_list: proxies = { 'http': item, 'https': item, } print(proxies) try: requests.get('目标网站', proxies=proxies, timeout=1) print('ok') except: continue

123456789101112131415161718192021222324252627282930

相关知识

用Python爬虫获取网络园艺社区植物养护和种植技巧
基于Python爬虫的电商网站彩妆数据的分析与研究
【零基础】Python3学习课后练习题(十)
爬虫技术在搜索引擎优化(SEO)中的应用与实践
python3炫酷玫瑰花与爱心表白源代码
京东爬虫
Python3入门基础:第二篇(注释、变量、input函数)
一个简单的python网路爬虫示例——爬取《后来的我们》影评
在Applet中调用RMI对象实现分布式网络计算
Python爬虫基本流程梳理:构造请求头、发起请求、json格式数据解码转码、读写CSV文件——基于AJAX网页爬虫

网址: Python3网络爬虫(三) https://m.huajiangbk.com/newsview1108869.html

所属分类:花卉
上一篇: 孔德昕:广东几乎用6.5人轮换击
下一篇: 交替和轮换使用杀虫剂农药,延缓抗