#!/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