【问题标题】:Different results between headless mode and no headless mode python selenium无头模式和无头模式python selenium之间的不同结果
【发布时间】:2021-02-05 17:07:08
【问题描述】:

我正在使用 chromedriver、selenium 和 BeatifulSoup 抓取以下网页:

https://www.rappi.com.co/tiendas/exito-express/s?store_type=express_exito&query=man%C3%AD&search_type=TYPED&origin=general

我使用 selenium 与网页交互,在确定显示整个网页后,我使用 BeatifulSoup 来定位和提取信息

我使用 BeatifulSoup 寻找这个元素

img = n.find("img", {"class":"ng-lazyloading"})["src"]

当我在有头(无头)模式下运行脚本时,我得到了所有 src 属性,但是当我使用无头模式运行脚本时,我只得到了最后一个 src。

如何使用无头模式获取整个 src 属性。

这是设置的参数:

options = webdriver.ChromeOptions()
options.add_argument("--incognito")
options.add_argument("--headless")
options.add_argument('--no-sandbox')
options.add_argument('window-size=1051x806')
#options.add_argument("start-maximized")
# options.use_chromium = True
prefs = {"profile.managed_default_content_settings.images": 2}
options.add_experimental_option("prefs", prefs)

driver = webdriver.Chrome(executable_path= ChromeDriverManager().install(), options=options)
driver.set_page_load_timeout(30)
driver.implicitly_wait(10)


look at the output

【问题讨论】:

    标签: python selenium beautifulsoup


    【解决方案1】:

    也许您可以尝试使用 selenium 驱动程序本身来获取 src 链接,

    enter code here
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.ui import WebDriverWait
    
    img=[]
    
    l=WebDriverWait(driver, 60).until(EC.visibility_of_all_elements_located((By.XPATH,"//img[@class='  ng-lazyloaded']")))
    
    for i in l:
        img.append(i.get_attribute('src'))
    

    【讨论】:

    • 不工作,adittionally它应该是一个beautifulsoup解决方案,因为我通过这种方式获得了很多元素
    猜你喜欢
    • 1970-01-01
    • 2021-12-29
    • 2018-01-20
    • 1970-01-01
    • 2022-12-18
    • 2023-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多