python3 爬取小姐姐图片

使用python3直接运行即可,可以自己切换url

import requests
import re
import time
import os
# 模仿浏览器访问
headers = {
    'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36'
}
#网站地址
response = requests.get('https://www.vmgirls.com/13138.html',headers=headers)
html = response.text
#提取图片标题
dir_name = re.findall('<h1 class="post-title h3">(.*?)</h1>',html)[-1]
#判断文件夹是否存在
if not os.path.exists(dir_name):
    os.mkdir(dir_name)
#爬取图片地址 <a href="https://static.vmgirls.com/image/2019/12/2019122209234029-scaled.jpeg" alt="初恋粉色系" title="初恋粉色系">
urls = re.findall('<a href="(.*?)" alt=".*?" title=".*?">',html)
print(urls)
for url in urls:
#提取图片名字
    file_name = url.split('/')[-1]
    response = requests.get(url,headers=headers)
#新建文件夹以图片标题命名,并保存图片
    with open(dir_name + '/' + file_name,'wb')as f:
        f.write(response.content)

猜你喜欢

转载自www.cnblogs.com/xyongsec/p/12108546.html