Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x trip advisor使用selenium,无法使用find_elements_by_classname selenium获取具有相同类名的所有图像_Python 3.x_Selenium_Web Scraping - Fatal编程技术网

Python 3.x trip advisor使用selenium,无法使用find_elements_by_classname selenium获取具有相同类名的所有图像

Python 3.x trip advisor使用selenium,无法使用find_elements_by_classname selenium获取具有相同类名的所有图像,python-3.x,selenium,web-scraping,Python 3.x,Selenium,Web Scraping,我面临的问题是我在使用 driver.find_elements_by_classname("a_classname_common_to_all_images_in_tripadvisor_hotels") 然而,每次运行脚本时,我得到的结果都不一样。 例如,有时它会在页面的30个部分中删除前5个,有时是4/30,以此类推。 我正在从这个链接中抓取图像: 我能够使用class_name方法找到所有酒店的名称,但对于图像,它是可变的。 非常感谢您的帮助,谢谢:)来自 引导w

我面临的问题是我在使用

driver.find_elements_by_classname("a_classname_common_to_all_images_in_tripadvisor_hotels")
然而,每次运行脚本时,我得到的结果都不一样。 例如,有时它会在页面的30个部分中删除前5个,有时是4/30,以此类推。 我正在从这个链接中抓取图像:

我能够使用class_name方法找到所有酒店的名称,但对于图像,它是可变的。 非常感谢您的帮助,谢谢:)

来自

引导webdriver等待加载所有元素

images = WebDriverWait(driver, 30).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "_1a4WY7aS")))
进口

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

当它只抓取了5张图像时,意味着只加载了5张图像。您应该做两件事来获取页面上的每个图像

  • 向下滚动至页面末尾:您可以通过选择body元素,然后发送向下键来完成此操作

    from selenium.webdriver.common.keys import Keys
    import time
    
    for _ in range(10):
        driver.find_element_by_tag_name("body").send_keys(Keys.PAGE_DOWN)
        time.sleep(0.2)
    
  • 滚动后,等待元素出现

    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    
    WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,
     "img._1a4WY7aS")))
    
  • from selenium.webdriver.common.keys import Keys
    import time
    
    for _ in range(10):
        driver.find_element_by_tag_name("body").send_keys(Keys.PAGE_DOWN)
        time.sleep(0.2)
    
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    
    WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,
     "img._1a4WY7aS")))