Python Bulk Download photos

Source: Internet
Author: User

#!/usr/bin/python# _*_ coding:utf-8 _*_ ' ' Created on August 22, 2018 ' # import Package Imports Urllibimport Urllib2import reimport os# Get net Station Address req = Urllib2. Request (' https://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gb18030 &word=%c1%f5%ca%ab%ca%ab&fr=ala&ala=1&alatpl=star&pos=0&hs=2&xthttps=111111 ') # Open Web site , and assign to the object FF = Urllib2.urlopen (req) # set the local download path, note the following \ \, the first one is the escape character, the second is the \ character of the path Localdir = ' E:\download\\ ' # defines an empty list urllist = []# Traverse Web Site # To determine if a local path exists, create if not os.path.exists (Localdir): Os.makedirs (Localdir) to Eachline in F: # The head and tail of each line are stripped of the space lines = EAC Hline.strip () # matches the line containing the JPG, (. *) Explanation:. Any character does not contain a newline, *0-n if Re.match ('. *jpg.* ', line): # print (line) # test used, can be annotated to print a matching string # to Double quotation marks as delimiters, each line is split by double quotation marks wordList = line.split (' \ "') for Word in WordList: # After the break, match the beginning of HTTPS and the end of the JPG string if Re.match (' https:/.* . jpg ', Word): # Appends the previous step of the matched string to the defined empty list urllist in Urllist.append (word) # to go back to the list, because subsequent discoveries have duplicates urllist = List (set (urllist)) #去重 # Traverse the list of everyfile in Urllist:everyurl = everyfile # will URL follow/slice, then get last string as photo name LocalFilename = everyurl.split ('/') [ -1]# print (localfilename) # test used, can be annotated, purpose is Print a matching string # Stitch string, define the absolute path to the local Photo: directory path + photo name LocalFile = Localdir + localfilename# print (localfile) # test used, can be annotated to print matching string # t RY syntax, use the Urllib.urlretrieve method to download a photo to a local Try:urllib.urlretrieve (Everyurl, LocalFile) #按照url进行下载 and store it in a local directory with its file name except Exception,e:continue

Python bulk Download photos

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.