首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何用Python中的Javascript生成页面?

如何用Python中的Javascript生成页面?
EN

Stack Overflow用户
提问于 2018-08-27 01:19:17
回答 1查看 0关注 0票数 0

我想下载生成的网页Javascript,并将其存储在Python代码中的字符串变量中,单击按钮时会生成页面,如果我知道我会使用的结果URL,urllib2但事实并非如此

EN

回答 1

Stack Overflow用户

发布于 2018-08-27 10:33:27

你可以用Selenium Webdriver:

代码语言:txt
复制
#!/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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100006054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档