我正在做《用 Python 自動化無聊的東西》一書中提到的一個專案。任務是從 flicks 或 imgur 下載特定搜索標簽的所有影像。所以我選擇了 flickr,但即使它已經定義,我的超時例外也不起作用。
#! python3
# download all images from Flicker using user defined Tag
import logging, os, time, requests
import pyinputplus as pyip
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')
# Ask user for a Tag
print("Hello user, this utility helps you to download images of specific 'tag' to your PC.\n")
tag = pyip.inputStr("Please choose a tag:", allowRegexes=[r'\s'])
os.makedirs(tag, exist_ok=True) # creates folder named as user define tag
# get url
baseurl = "https://www.flickr.com/"
search_prefix= "search/?text="
search_suffix= "&media=photos"
search_url = baseurl search_prefix tag search_suffix
logging.debug(f'{search_url}')
# opens firefox and get list of image links
driver = webdriver.Firefox()
driver.get(search_url)
time.sleep(2)
item_links = []
for item in driver.find_elements_by_class_name('overlay'):
item_links.append(item.get_attribute('href'))
print('Python script has found %s images.\n' % (len(item_links)))
count = pyip.inputInt('How many images would you like to download?')
# getting all links for images
image_links = []
for i in range(0,count):
image_link = str(item_links[i]) 'sizes/l/'
logging.debug(f'{image_link}')
driver.get(image_link)
try:
webelement = WebDriverWait(driver,5).until(EC.presence_of_element_located((By.CSS_SELECTOR,'#allsizes-photo > img:nth-child(1)')))
image_links.append(webelement.get_attribute('src'))
except TimeoutException as e:
pass
# downloading all images
for link in image_links:
response = requests.get(link)
response.raise_for_status()
file = open(os.path.join(tag,os.path.basename(link)), 'wb')
for chunk in response.iter_content(100000):
file.write(chunk)
file.close()
print('Done.')
一切正常,直到由于下載限制而找不到 webelement,即使定義了“除了 TimeoutExceptions”。我發現 css_selector 從 #allsizes-photo > img:nth-child( 1 ) (如果存在)更改為 #allsizes -photo > img:nth-child( 2 ) (如果沒有)但我的印象是例外應該能夠處理那個。你能告訴我我做錯了什么嗎?
根據要求鏈接示例:
webelement 呈現 flickr.com/photos/bjarnekosmeijer/48718018992/sizes/l
timeoutexception flickr.com/photos/ebanatawka/5062862631/sizes/l
例外
網路元素存在
uj5u.com熱心網友回復:
我正在使用這個 CSS
div#allsizes-photo img
與visibility_of_element_located在明確的等待
代碼 :
driver.get("https://www.flickr.com/photos/bjarnekosmeijer/48718018992/sizes/l/")
webelement = WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.CSS_SELECTOR,"div#allsizes-photo img")))
print(webelement.get_attribute('src'))
輸出
https://live.staticflickr.com/65535/48718018992_7cbf09802a_b.jpg
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/362306.html
