本文案例只供练习学习,不做其他用途!
在这里插入代码片
```import requests
import os
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'}
m=1
def true_text(n1):
global headers
url=f'https://bbs-api.mihoyo.com/post/wapi/getForumPostList?forum_id=47&gids=5&is_good=false&is_hot=false&last_id={n1}&page_size=20&sort_type=2'
resp1=requests.get(url,headers=headers)
resp=resp1.json()
return resp
def true_url(true_resp):
if true_resp['retcode']==0:
trueurl=true_resp['data']['list']
for trueurl1 in trueurl:
trueurl2=trueurl1['post']['images']
for trueurl3 in trueurl2:
if(trueurl3.find('weigui'))<0:
storage(trueurl3)
def storage(url):
global headers
global m
file=os.path.exists('漫展图')
if not file:
try:
os.mkdir('漫展图')
except NotADirectoryError:
print('漫展图已经创建成功')
resp=requests.get(url,headers=headers)
file1 =os.path.join('漫展图','第'+str(m)+'张'+'.jpg')
with open(file1,'wb') as f:
f.write(resp.content)
print('第'+str(m)+'张'+'保存成功!')
m+=1
def main(n):
global header
url=f'https://bbs-api.mihoyo.com/post/wapi/getForumPostList?forum_id=47&gids=5&is_good=false&is_hot=false&last_id={n}&page_size=20&sort_type=2'
resp1=requests.get(url,headers=headers)
resp=resp1.json()
if resp['retcode'] == 0:
n1 = resp['data']['last_id']
true_resp=true_text(n1)
true_url(true_resp)
print(n1)
return main(n1)
if __name__ == '__main__':
main(6716779)
|