'''
Created on 2017年5月11日
根據用戶輸入的圖片網站地址,關鍵詞搜索後,下載搜索結果的全部圖片
@author: Nick
'''
from selenium import webdriver
import requests,bs4,os
imageUrlList = {}
pageNum = 1
#
#例如圖片網站:http://www.123rf.com.cn/
url = 'http://www.123rf.com.cn/'
#打開圖片網站:http://www.123rf.com.cn/
downLoadImage = webdriver.Firefox()
downLoadImage.get(url)
#關鍵詞搜索圖片
selectKeyWord = input('Please enter select keyword:')
downLoadImage.find_element_by_name('keyword').clear()
downLoadImage.find_element_by_name('keyword').send_keys('美人')
#通過相對路徑定位到搜索按鈕
downLoadImage.find_element_by_xpath("//form[@id='search-form']/div/span/button").click()
print(downLoadImage.find_elements_by_xpath("//div['class=gallery-item-thumb-content']"))
#http://www.123rf.com.cn/search.php?keyword=%E7%BE%8E%E4%BA%BA&page=1
#下載當前列表頁,並取得圖片名稱和url的鍵值對,存放在字典表中
def baoCunImageUrl(keyWord,pageNum):
urlSearch = url + 'search.php?keyword=' + keyWord +'&page=' + str(pageNum)
resImageUrl = requests.get(urlSearch)
try:
resImageUrl.raise_for_status()
except Exception as exc:
print('There was a problem:%s'%(exc))
# print(resImage.text)
bs4ImageDownLoadUrl = bs4.BeautifulSoup(resImageUrl.text)
for i in range(len(bs4ImageDownLoadUrl.select('.uitooltip'))):
imageUrlList[bs4ImageDownLoadUrl.select('.uitooltip')[i].get('picid')] = bs4ImageDownLoadUrl.select('.uitooltip')[i].get('src')
#循環遍歷列表所有頁面取出全部圖片的標題和url
while True:
if downLoadImage.find_elements_by_xpath("//div/a['class=btn btn-search-pagination-next']"):
baoCunImageUrl(selectKeyWord,pageNum)
downLoadImage.find_elements_by_xpath("//div/a['class=btn btn-search-pagination-next']")[-1].click()
pageNum = pageNum + 1
else:
break
#循環遍歷字典下載圖片並保存在本地
for k,v in imageUrlList.items():
resImage = requests.get(str(v))
openImage = open('C:\\Users\\Nick\\Desktop\\python\\drawing\\2\\image\\' + str(k) +'.jpg','ab' )
for chunk in resImage.iter_content(10000):
openImage.write(chunk)
openImage.close()
Python+selenium實現圖片網站搜索後下載搜索結果的全部照片
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.