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

标签 javascript python html download urllib2

我想下载 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/

相关文章:

javascript - 在 Canvas 中绘制多个圆弧的 JQuery 动画问题

python - 由于某种原因无法在Python2.7中调用Tkinter模块

python - 在 Pandas 数据框中取消旋转具有相同名称的多列

html - 动态表中的固定宽度列

javascript - jQuery $.get() 内存使用情况

javascript - Div + JQuery 不清除

Javascript Math.cos 和 Math.sin 不准确。有什么解决办法吗?

python - Python 3.1 中的 GIL

javascript - 将样式属性迁移到单独的 .css 文件

javascript - 在发送 ajax 同步请求之前显示 html 元素