Python bulk download images from Web pages

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.