请注意这个问题仍然是打开的,因为建议的“答案”仍然提供相同的输出,因为它不能解释为什么JS不在该页面上运行,或者为什么selenium不能提取它
我正在尝试读取由JS呈现的:http://147.235.97.36/ (Hp打印机)的页面源。
所以我写到:
driver.get(url)
wait_for_page(driver)
source = driver.page_source
print(source)但在印刷品中,我看到:
<p>JavaScript is required to access this website.</p>
<p>Please enable JavaScript or use a browser that supports JavaScript.</p>而且有些内容不存在,所以我将代码更改为:
driver.get(url)
wait_for_page(driver)
source = driver.execute_script("return document.getElementsByTagName('html')[0].innerHTML")
print(source)同样的输出,你能帮我理解这里的问题吗?
下面是我的init_driver函数:
def init_driver():
# --Initialize Driver--#
chrome_options = Options()
chrome_options.add_argument("--headless") # Run in Background
chrome_options.add_argument('--disable-gpu') if os.name == 'nt' else None # Windows workaround
prefs = {"profile.default_content_settings.images": 2,
"profile.managed_default_content_settings.images": 2} # Disable Loading of Images
chrome_options.add_experimental_option("prefs", prefs)
chrome_options.add_argument('--ignore-ssl-errors=yes')
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument("--window-size=1920,1080") # Standard Window Size
chrome_options.add_argument("--pageLoadStrategy=normal")
driver = None
try:
driver = webdriver.Chrome(options=chrome_options, service=Service('./chromedriver'))
driver.set_page_load_timeout(REQUEST_TIMEOUT)
except Exception as e:
log_warning(str(e))
return driver发布于 2022-06-08 14:32:42
您可以添加一些参数以避免geting detected,并按如下方式打印:
选项= Options() options.add_experimental_option("excludeSwitches",(“启动-最大化”)options.add_argument“启用-自动化”) options.add_experimental_option('excludeSwitches',‘启用-日志’) options.add_experimental_option('useAutomationExtension',False) options.add_argument('--disable-blink-features=AutomationControlled') s=服务(‘C:\BrowserDrivers\chromedriver.exe’)驱动程序= webdriver.Chrome(service=s ),( options=options) driver.get(“http://147.235.97.36/"”)打印(driver.execute_script)(“返回driver.get”)
https://stackoverflow.com/questions/72544798
复制相似问题