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

python 批量下载网页里的图片

时间:2018-01-12 13:38:33      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:ade   note   end   取图   not   linu   head   列表   gre   

python 3.*

import requests
import sys,re
#设置提取图片url 的正则表达式
imgre = re.compile(r"<img id=\"imgis\" src=‘(.*?)‘")
#存放找到的 图片url的列表
all_img_urls = []
#图片下载后存放位置
save_path = r‘/root‘
#获取指定网页中的图片url
def get_img_url(tmpurl,tmpre,allimgurl,timeout=10):
headers = {‘User-Agent‘: ‘Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36‘}
link = requests.get(tmpurl,headers=headers,timeout = timeout)
content = link.text
retsults = tmpre.findall(content)
for i in retsults:
allimgurl.append(i)
#对图片url进行下载保存
def save_img(tmpurl,tmppath,timeout=10):
headers = {‘User-Agent‘: ‘Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36‘}
ret = requests.get(tmpurl,headers=headers,timeout=timeout)
img = ret.content
filename = ‘{}/{}‘.format(tmppath,tmpurl.split(‘/‘)[-1])
with open(filename,‘wb‘) as f:
f.write(img)

if name == ‘main‘:
urls = [‘http://www.ivsky.com/bizhi/death_note_v17111/pic_{}.html#al_tit‘.format(385791+x) for x in range(10)]
try:
for url in urls:
imgurls = get_img_url(url, imgre, all_img_urls)
except Exception as e:
print(e)

for img in all_img_urls:
    try:
        save_img(img,save_path)
    except Exception as e:
        print(e)

python 批量下载网页里的图片

标签:ade   note   end   取图   not   linu   head   列表   gre   

原文地址:http://blog.51cto.com/13308811/2060142

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