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)
原文地址:http://blog.51cto.com/13308811/2060142