python批量爬取图片

彭维盛

关注

阅读 110

2023-02-17


import requests
import time
import re\


# 请求网页
# header防止被禁止访问403,伪装成浏览器,不会被认为是python

headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'
}


response = requests.get('https://www.vmgirls.com//12985.html', headers=headers)
# print(response.request.headers)
html = response.text


# 解析网页

urls = re.findall('<a href=".*?" alt=".*?" title=".*?">', html)
print(urls)

# 保存图片

for url in urls:
time.sleep(1)
# 图片的名字,用split分割,找到最后一个即为文件名
file_name = url.split('/')[-1]
response = requests.get('https://www.vmgirls.com//12985.html', headers=headers)
#打开文件,wb二进制写入
with open(file_name, 'wb') as f:
f.write(response.content)

 

精彩评论(0)

0 0 举报