码迷,mamicode.com
首页 > 编程语言 > 详细

Python爬虫-爬取照片并保存

时间:2020-03-03 20:55:20      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:not   爬取   get   name   dal   imp   解析   print   rom   

"""请求网页"""
import requests
import re
import time
import os
headers={
user-agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537
}
response=requests.get(https://www.vmgirls.com/12985.html,headers=headers)
#print(response.request.headers)
#print(response.text)
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)
urls=re.findall(<a href="(.*?)" alt=".*?" title=".*?">,html)
print(urls)
"""保存图片"""
for url in urls:
    time.sleep(1)
    #图片的名字
    file_name=url.split(/)[-1]
    response = requests.get(url, headers=headers)
    with open(dir_name+/+file_name,wb) as f:
        f.write(response.content

 

Python爬虫-爬取照片并保存

标签:not   爬取   get   name   dal   imp   解析   print   rom   

原文地址:https://www.cnblogs.com/liujinxin123/p/12404308.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!