gpt4 book ai didi

python - 无法在 Selenium 中查看完整的页面源代码

转载 作者:太空宇宙 更新时间:2023-11-04 05:25:08 24 4
gpt4 key购买 nike

当我通过 Chrome 手动导航到站点后查看源 HTML 时,我可以看到完整的页面源代码,但是在通过 selenium 加载页面源代码时,我没有获得完整的页面源代码。

from bs4 import BeautifulSoup
from selenium import webdriver
import sys,time


driver = webdriver.Chrome(executable_path=r"C:\Python27\Scripts\chromedriver.exe")
driver.get('http://www.magicbricks.com/')


driver.find_element_by_id("buyTab").click()

time.sleep(5)
driver.find_element_by_id("keyword").send_keys("Navi Mumbai")

time.sleep(5)
driver.find_element_by_id("btnPropertySearch").click()

time.sleep(30)

content = driver.page_source.encode('utf-8').strip()

soup = BeautifulSoup(content,"lxml")

print soup.prettify()

最佳答案

该网站可能阻止或限制了 selenium 的用户代理。一个简单的测试是更改用户代理并查看是否这样做。有关此问题的更多信息:

Change user agent for selenium driver

引用:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opts = Options()
opts.add_argument("user-agent=whatever you want")

driver = webdriver.Chrome(chrome_options=opts)

关于python - 无法在 Selenium 中查看完整的页面源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39047079/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com