我想下载生成的网页Javascript,并将其存储在Python代码中的字符串变量中,单击按钮时会生成页面,如果我知道我会使用的结果URL,urllib2但事实并非如此
发布于 2018-08-27 10:33:27
你可以用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)
https://stackoverflow.com/questions/-100006054
复制相似问题