# 爬虫
import requests
import jsonpath
page_list = [i for i in range(1,4)]
img_list = []
headers = {
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
}
for page in page_list:
url = 'https://m.huya.com/cache.php?m-Game&do=ajaxGetGameLive&gameId=2168&page={}&pageSize=120'.format(page)
data = requests.get(url,headers=headers).json()
img_url_list = jsonpath(data,'&..screenshot')
for img_url in img_url_list:
img_list.append(img_url)
img_url_set = set(img_list)
for index,img_url in enumerate(img_url_set):
data = requests.get(img_url,headers).content
file_path = 'img/' + str(index + 1) + '.jpg'
with open(file_path,'wb') as f:
print('第{}张下载完成'.format((index + 1))
运行后:
Process finished with exit code 1