暂无分类
暂无标签
发布于2021-02-28 19:31 阅读(1014) 评论(1) 点赞(27) 收藏(1)
0
1
2
3
4
5
6
本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。
Python爬虫、数据分析、网站开发等案例教程视频免费在线观看
https://space.bilibili.com/523606542
Python爬虫新手入门教学(八):爬取论坛文章保存成PDF
Python爬虫新手入门教学(十五):Python爬取某音乐网站的排行榜歌曲
import requests # 第三方模块 import parsel import time # 时间模块
安装Python并添加到环境变量,pip安装需要的相关模块即可。
获取代理IP地址,端口然后对IP进行检测
网站是静态网页,是可以直接获取数据的。
根据re、xpath或者css选择器 都是可以提取数据的,还是比较简单的。爬取IP主要是因为在使用爬虫频繁抓取数据的时候,某些网站是比较容易被封IP的。
虽然网站有很多关于免费的IP代理可以使用,但是基本上都是用不了的。
import requests # 第三方模块 import parsel import time # 时间模块 def check_ip(proxies_list): """检测代理ip的可用性""" use_proxy = [] for ip in proxies_list: try: response = requests.get(url='https://www.baidu.com', proxies=ip, timeout=2) if response.status_code == 200: use_proxy.append(ip) except Exception as e: print('当前代理ip: ', ip, '请求超时, 检测不合格!!!') else: print('当前代理ip: ', ip, '检测通过') return use_proxy proxy_list = [] for page in range(1, 11): time.sleep(0.5) print(f'==================正在抓取第{page}页数据================') # 1.确定数据所在地址<url>(分析网页性质<静态网页\动态网页>) url = f'http://www.ip3366.net/?stype=1&page={page}' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'} # 2.发送网络请求 response = requests.get(url=url, headers=headers) html_data = response.text # str # print(html_data) # 3.解析数据 # 3.1 转换数据类型 selector = parsel.Selector(html_data) # 3.2 数据提取 trs = selector.xpath('//table[@class="table table-bordered table-striped"]/tbody/tr') # tr """ # 代理ip的结构 proxies_dict = { "http": "http://" + ip:端口, "https": "http://" + ip:端口, } """ for tr in trs: ip_num = tr.xpath('./td[1]/text()').get() ip_port = tr.xpath('./td[2]/text()').get() # print(ip_num, ip_port) ip_proxy = ip_num + ':' + ip_port # print(ip_proxy) proxies_dict = { 'http': "http://" + ip_proxy, 'https': "https://" + ip_proxy } # 4.数据的保存 proxy_list.append(proxies_dict) print('保存成功:', proxies_dict) print(proxy_list) print('获取到的代理ip数量: ', len(proxy_list)) print('============================正在检测代理===================================') can_use = check_ip(proxy_list) print('可用代理:', can_use) print('可用代理数量:', len(can_use))
爬取了100IP代理,最终可以使用的就只有一个,事实证明还是付费的香
0
1
2
3
4
5
作者:从新来过
链接: https://www.pythonheidong.com/blog/article/864466/e3f07140845fc3d39cc3/
来源: python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
提供高质量代理ip,免费测试 829246420
昵称:
评论内容:(最多支持255个字符)
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!