python批量下載照片

#!/usr/bin/python # _*_ coding: utf-8 _*_ ''' Created on 2018年8月22日 ''' # 導入包 import urllib import urllib2 import re import os # 獲取網站地址 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') # 打開網站,並賦給對象f f = urllib2.urlopen(req) # 設置本地下載路徑,注意後面的\\,第一個是轉義字符,第二個是路徑的\字符 localDir = 'E:\download\\' # 定義空列表 urlList = [] # 遍歷網站 # 判斷本地路徑是否存在,如果不存在就創建 if not os.path.exists(localDir): os.makedirs(localDir) for eachLine in f: # 每一行的頭和尾去掉空格 line = eachLine.strip() # 匹配含有jpg的行,(.*) 解釋: .任意字符不包含換行,*0-N次 if re.match('.*jpg.*', line): # print(line) # 測試使用,可以註釋,目的是打印匹配的字符串 # 以雙引號作爲分隔符,將每一行按照雙引號進行拆分 wordList = line.split('\"') for word in wordList: # 拆之後,匹配https開頭和jpg結尾的字符串 if re.match('https:/.*.jpg', word): # 將上一步匹配的字符串追加到定義的空列表urlList中 urlList.append(word) # 將列表去重,因爲後續發現有重複現象 urlList = list(set(urlList)) #去重 # 遍歷去重後的列表 for everyFile in urlList: everyURL = everyFile # 將url按照 / 切分,然後獲取最後一個字符串作爲照片名稱 localFileName = everyURL.split('/')[-1] # print(localFileName) # 測試使用,可以註釋,目的是打印匹配的字符串 # 拼接字符串,定義本地照片的絕對路徑:目錄路徑+照片名字 localFile = localDir + localFileName # print (localFile) # 測試使用,可以註釋,目的是打印匹配的字符串 # try語法,利用urllib.urlretrieve方法,將照片下載至本地 try: urllib.urlretrieve(everyURL, localFile) #按照url進行下載,並以其文件名存儲到本地目錄 except Exception,e: continue
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章