无头模式和无头模式的结果不同

2024-05-03 10:57:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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"]

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

如何使用headless模式获取整个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

enter image description here


Tags: srcadd网页属性typedriverselenium模式
1条回答
网友
1楼 · 发布于 2024-05-03 10:57:00

可能您可以尝试使用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'))

相关问题 更多 >