python爬虫练习高清壁纸【王者荣耀高清壁纸】
仅做练习使用,各位小伙伴不用乱来!!!
现成的代码,直接拿走研究,已经精简了自行美化
页面自己去分析分析 ,学习效果更佳。
import requests,os
import json
import time
from urllib import parse #使用parse.unquote 对url密文进行解码
def get_img():
for i in range(2,27): #一共有多少页需要获取
times = int(time.time()*1000) #需要获取一下时间戳
#print(times)
url = f'https://apps.game.qq.com/cgi-bin/ams/module/ishow/V1.0/query/workList_inc.cgi?activityId=2735&sVerifyCode=ABCD&sDataType=JSON&iListNum=20&totalpage=0&page={i}&iOrder=0&iSortNumClose=1&jsoncallback=jQuery171004809195531936239_1605753090427&iAMSActivityId=51991&_everyRead=true&iTypeId=2&iFlowId=267733&iActId=2735&iModuleId=2735&_={times}'#.format(times)
#print(url)
#请求头
headers = {
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3573.0 Safari/537.36'
}
html_text = requests.get(url=url,headers=headers).text
html_data = html_text[html_text.find('(')+1:-2]
html_data_json = json.loads(html_data)
hero_data_all = html_data_json['List']
try:
os.mkdir('王者荣耀')
print('王者荣耀文件夹创建成功')
except Exception as e:
print(e,'已经存在文件夹!程序继续运行')
finally:
for hero in hero_data_all:
# num = num + 1
time.sleep(5)#暂停几秒,免得被反爬
hero_url = parse.unquote(hero[f'sProdImgNo_6'].replace('200','0'))
hero_name = parse.unquote(hero['sProdName'])
#print(hero_url)
img_data = requests.get(url=hero_url,headers=headers).content
with open('王者荣耀\\'+hero_name+'.jpg','wb') as f:
f.write(img_data)
print(hero_name,'-'*10,'下载完成')
get_img()