在 Python 中获取用 Javascript 生成的页面 [英] Get page generated with Javascript in Python

查看:44
本文介绍了在 Python 中获取用 Javascript 生成的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to download web page generated by Javascript and store it to string variable in Python code. The page is generated when you click on button.

如果我知道结果 URL,我会使用 urllib2 但事实并非如此.

If I would know the resulting URL I would use urllib2 but this is not the case.

谢谢

推荐答案

你可以使用 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)

这篇关于在 Python 中获取用 Javascript 生成的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆