Python 为特定元素导入WebDriverWait
这个问题是我上一个问题()的后续问题。我正在从grailed.com()上抓取所有Air Jordan数据。我将大小、型号、url和图像url存储在一个对象中。我目前有一个程序可以滚动整个提要并获取所有这些内容。除了查找图像url之外,其他一切都正常工作。@KunduK建议,图像URL似乎需要显式等待。我正在尝试实现他的解决方案,这样我就可以提取for循环中的每个图像:Python 为特定元素导入WebDriverWait,python,selenium,selenium-webdriver,web-scraping,wait,Python,Selenium,Selenium Webdriver,Web Scraping,Wait,这个问题是我上一个问题()的后续问题。我正在从grailed.com()上抓取所有Air Jordan数据。我将大小、型号、url和图像url存储在一个对象中。我目前有一个程序可以滚动整个提要并获取所有这些内容。除了查找图像url之外,其他一切都正常工作。@KunduK建议,图像URL似乎需要显式等待。我正在尝试实现他的解决方案,这样我就可以提取for循环中的每个图像: while True and len(sneakers) < sneaker_count: driver.ex
while True and len(sneakers) < sneaker_count:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Get sneakers currently on page and add to sneakers list
feed = driver.find_elements_by_class_name('feed-item')
images = WebDriverWait(driver, 10).until(
EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".feed-item .listing-cover-photo>img")))
for item in feed:
...
而True和len(运动鞋)img”))
对于提要中的项目:
...
当前,代码一次获取组中的图像。我试图在“for item in feed”块中获取图像。我想要像
images=WebDriverWait(driver,10).until(EC.visibility\u of_elements\u located((By.SOME SELECTOR)”,???)
这样的东西,但我真的不知道如何使用'item'元素找到它们。有人能帮我吗?使用从每个图像中刮取图像url,你必须为visibility\u of_elements\u located()
您可以使用以下任一选项:
- 使用
:CSS\u选择器
driver.get('https://www.grailed.com/designers/jordan-brand/hi-top-sneakers') print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "a.product-card-container")))])
- 使用
:XPATH
driver.get('https://www.grailed.com/designers/jordan-brand/hi-top-sneakers') print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//a[@class='product-card-container']")))])
- 控制台输出:
['https://www.grailed.com/products/57773-jordan-brand-air-jordan-1-retro-high-og-court-purple', 'https://www.grailed.com/products/57803-jordan-brand-air-jordan-1-retro-high-og-obsidian', 'https://www.grailed.com/products/57759-jordan-brand-air-jordan-1-retro-high-og-2017-royal', 'https://www.grailed.com/products/57760-jordan-brand-air-jordan-1-retro-high-og-2018-shadow', 'https://www.grailed.com/products/59036-jordan-brand-air-jordan-4-retro-og-2019-bred', 'https://www.grailed.com/products/115772-jordan-brand-jordan-1-retro-high-og-pine-green', 'https://www.grailed.com/products/57817-jordan-brand-air-jordan-1-retro-high-og-shattered-backboard-3-0', 'https://www.grailed.com/products/61668-jordan-brand-travis-scott-travis-scott-x-air-jordan-4-retro-cactus-jack', 'https://www.grailed.com/products/114979-jordan-brand-air-jordan-1-retro-high-og-unc-to-chi', 'https://www.grailed.com/products/97122-jordan-brand-air-jordan-1-retro-high-og-fearless', 'https://www.grailed.com/products/97133-jordan-brand-air-jordan-11-bred-2019', 'https://www.grailed.com/products/61725-jordan-brand-air-jordan-4-retro-cool-grey', 'https://www.grailed.com/products/57762-jordan-brand-air-jordan-1-retro-high-og-banned-2016-banned-bred', 'https://www.grailed.com/products/87098-jordan-brand-travis-scott-travis-scott-x-air-jordan-6-retro-olive', 'https://www.grailed.com/products/57768-jordan-brand-air-jordan-1-retro-high-og-bred-toe', 'https://www.grailed.com/products/112831-jordan-brand-air-jordan-1-retro-high-og-royal-toe', 'https://www.grailed.com/products/111383-jordan-brand-air-jordan-4-retro-black-cat-2020', 'https://www.grailed.com/products/58136-jordan-brand-travis-scott-travis-scott-x-air-jordan-1-retro-high-og-mocha', 'https://www.grailed.com/products/57825-jordan-brand-air-jordan-1-retro-high-og-turbo-green', 'https://www.grailed.com/products/111377-jordan-brand-off-white-air-jordan-5-retro-sp-muslin']
- 注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
之后:img=item.get\u attribute(“src”)
但是在这个语句中使用WebDriverWait。有什么方法可以做到这一点吗?这很有帮助(给出了向上的投票),但我仍然停留在同一个问题上。我使用了您的xpath策略,并实现了一些其他东西,如果您能提供帮助,那将是非常好的—有关详细信息,请参阅我的最新问题。