gpt4 book ai didi

javascript - 获取在 Python 中使用 Javascript 生成的页面

转载 作者:IT老高 更新时间:2023-10-28 22:18:44 26 4
gpt4 key购买 nike

我想下载 Javascript 生成的网页并将其存储到 Python 代码中的字符串变量中。单击按钮时会生成页面。

如果我知道生成的 URL,我会使用 urllib2 但事实并非如此。

谢谢

最佳答案

您可以使用 Selenium Webdriver :

#!/usr/bin/env python
from contextlib import closing
from selenium.webdriver import Firefox # pip install selenium
from selenium.webdriver.support.ui import WebDriverWait

# use firefox to get page with javascript generated content
with closing(Firefox()) as browser:
browser.get(url)
button = browser.find_element_by_name('button')
button.click()
# wait for the page to load
WebDriverWait(browser, timeout=10).until(
lambda x: x.find_element_by_id('someId_that_must_be_on_new_page'))
# store it to string variable
page_source = browser.page_source
print(page_source)

关于javascript - 获取在 Python 中使用 Javascript 生成的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8960288/

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