给,加一个 Referer 就行了。
你只需要更改图片网址,header不用动。自动创建目录写好了,放进循环就自己做吧。
import requests
import os
# 你只需要改这儿
image_url = 'https://www.zgbk.com/upload/wordresources/wikiimage/2018/12/21/612481545397139858_thumbnail.png'
current_path = image_url[image_url.find('wikiimage/')+10:]
os.makedirs('/'.join(current_path.split('/')[:-1]))
with open(current_path, 'wb',) as f:
r = requests.get(image_url,
headers={'Referer':'https://www.zgbk.com/ecph/words?SiteID=1&ID=180450&Type=bkzyb&SubID=138634'})
if r.status_code == 200:
f.write(r.content)
print('success')
else:
print(r.text)