前言
本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。
PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取
基本环境配置
- python 3.6
- pycharm
- requests
相关模块pip安装即可
确定目标网页
分析网页
1、查看图片地址
代码实现
import requests
import parsel
url = 'https://bcy.net/coser/toppost100'
response = requests.get(url=url, headers=headers)
selector = parsel.Selector(response.text)
urls = selector.css('.rank-index-box .rank-cos-top img::attr(src)').getall()
titles = selector.css('.rank-index-box .rank-cos-avatar-box p::text').getall()
lis = zip(urls, titles)
for i in lis:
img_url = i[0]
title = i[1]
response_2 = requests.get(url=img_url, headers=headers)
path = 'D:\\python\\demo\\cos小姐姐\\img\\' + title + '.jpg'
with open(path, mode='wb') as f:
f.write(response_2.content)
print(img_url)
图片数据是爬取下来了,但是保存数据后发现,仅仅只有20张图片,这根本不是我想要的~
所以要重新分析网页了~看一下是否有接口数据
我清空所有加载出来的数据,下拉的时候,然后出现一个神秘地址,点击去一看,果然是接口数据。为了保证准确性,咱们可以多往下翻翻
果然~~~
通过链接对比,发现接口数据的参数P是改变的,等同于页数编码,1就是对应的前20张图片数据
代码实现
import requests
for page in range(1, 6):
url = 'https://bcy.net/apiv3/rank/list/itemInfo'
params = {
'p': '{}'.format(page),
'ttype': 'cos',
'sub_type': 'week',
'date': '20200922',
'_signature': 'a2ovWQAAAAAem6QHkeiiVmtqL0AADQG',
}
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
}
'''
Json 接口数据
'''
response = requests.get(url=url, params=params, headers=headers)
html_data = response.json()
info_list = html_data['data']['top_list_item_info']
for i in info_list:
img_url = i['item_detail']['cover']
title = img_url.split('/')[-1].split('~')[0]
response_2 = requests.get(url=img_url, headers=headers)
path = 'D:\\python\\demo\\cos小姐姐\\img\\' + title + '.jpg'
with open(path, mode='wb') as f:
f.write(response_2.content)
print(img_url)