本篇内容主要讲解“怎么用Python爬取王者荣耀皮肤”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用Python爬取王者荣耀皮肤”吧!
Python 3.6
Pycharm
import requests # 第三方模块
import parsel
import time # 时间模块
安装Python并添加到环境变量,pip安装需要的相关模块即可。
根据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))
到此,相信大家对“怎么用Python爬取王者荣耀皮肤”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/4848094/blog/4974433