Python 3.*
Import Requests
Import Sys,re
#设置提取图片url Regular expression
Imgre = Re.compile (r " List of #存放找到的 picture URLs
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 bulk download images from Web pages