Python: Download all images from a specified URL

Source: Internet
Author: User
This article describes how to download all images from a specified URL in python. It involves Python's skills in page reading, traversing, and file operations. It has some reference value, for more information about how to download all images from a specified URL in python, see the following example. Share it with you for your reference. The specific implementation method is as follows:

#coding=utf-8#download pictures of the url#useage: python downpicture.py www.baidu.comimport osimport sysfrom html.parser import HTMLParserfrom urllib.request import urlopenfrom urllib.parse import urlparsedef getpicname(path):  '''  retrive filename of url    '''  if os.path.splitext(path)[1] == '':    return None  pr=urlparse(path)  path='http://'+pr[1]+pr[2]  return os.path.split(path)[1]def saveimgto(path, urls):  '''  save img of url to local path  '''  if not os.path.isdir(path):    print('path is invalid')    sys.exit()  else:    for url in urls:      of=open(os.path.join(path, getpicname(url)), 'w+b')      q=urlopen(url)      of.write(q.read())      q.close()      of.close()class myhtmlparser(HTMLParser):  '''put all src of img into urls'''  def __init__(self):    HTMLParser.__init__(self)    self.urls=list()    self.num=0  def handle_starttag(self, tag, attr):    if tag.lower() == 'img':      srcs=[u[1] for u in attr if u[0].lower() == 'src']      self.urls.extend(srcs)      self.num = self.num+1if __name__ == '__main__':  url=sys.argv[1]  if not url.startswith('http://'):    url='http://' + sys.argv[1]  parseresult=urlparse(url)  domain='http://' + parseresult[1]  q=urlopen(url)  content=q.read().decode('utf-8', 'ignore')  q.close()  myparser=myhtmlparser()  myparser.feed(content)  for u in myparser.urls:    if (u.startswith('//')):      myparser.urls[myparser.urls.index(u)]= 'http:'+u    elif u.startswith('/'):      myparser.urls[myparser.urls.index(u)]= domain+u  saveimgto(r'D:\python\song', myparser.urls)  print('num of download pictures is {}'.format(myparser.num))

The running result is as follows:

Num of download pictures is 19

I hope this article will help you with Python programming.

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.