本帖最后由 YunGuo 于 2021-5-16 18:52 编辑
headers里面加一个refererheaders = {
'referer': 'https://www.vmgirls.com/',
'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'
}
你的代码这样改下"""请求网页"""
import requests
import re
headers = {
'referer': 'https://www.vmgirls.com/',
'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'
}
response = requests.get('https://www.vmgirls.com/12985.html', headers=headers)
html = response.text
"""解析网页"""
urls = re.findall('<a href="(.*?)" alt=".*?" title=".*?">', html)
# print(urls)
"""保存图片"""
for url in urls:
# # 图片的名字
file_name = url.split('/')[-1]
img_url = 'https:' + url
response = requests.get(img_url, headers=headers)
with open(file_name, 'wb') as f:
f.write(response.content)
|