我正在尝试使用 selenium 从 OfferUp 搜索中提取所有图像,但我尝试了一堆不同的代码,但没有一个有效。我希望能够在另一个网站或弹出窗口中显示所有图像,以便我可以了解 OfferUp 上商品价格的最新信息。任何帮助表示赞赏。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
# Driver path
chrome_driver_path = 'C:\\Users\gues\\OneDrive\\Desktop\\PyFiles\\chromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_driver_path)
driver.maximize_window()
# Open webpage
wait = WebDriverWait(driver, 20)
openOfferUp = driver.get('https://offerup.com/search?q=dresser&DISTANCE=20&DELIVERY_FLAGS=p&PRICE_MAX=100')
#retrieve images
time.sleep(20)
images = driver.find_elements((By.TAG_NAME, 'img'))
image_urls = [image.get_attribute('src') for image in images]
for url in image_urls:
print(url)
time.sleep(1000)
你可以尝试这个:img_urls = [img_element.get_attribute("src") for img_element in driver.find_elements(By.XPATH, {img_xpath})]