如何使用Selenium获取渲染后的HTML源代码
我在一个网页上运行了一个查询,然后得到了结果的链接。如果我右键点击查看 HTML 源代码,我可以看到由 JavaScript 生成的 HTML 代码。但是如果我直接使用 urllib,Python 是无法获取到这些 JavaScript 代码的。所以我看到一些解决方案是使用 selenium。以下是我的代码:
from selenium import webdriver
url = 'http://www.archives.com/member/Default.aspx?_act=VitalSearchResult&lastName=Smith&state=UT&country=US&deathYear=2004&deathYearSpan=10&location=UT&activityID=9b79d578-b2a7-4665-9021-b104999cf031&RecordType=2'
driver = webdriver.PhantomJS(executable_path='C:\python27\scripts\phantomjs.exe')
driver.get(url)
print driver.page_source
>>> <html><head></head><body></body></html> Obviously It's not right!!
这是我在右键窗口中需要的源代码,(我想要的是 INFORMATION 部分)
</script></div><div class="searchColRight"><div id="topActions" class="clearfix
noPrint"><div id="breadcrumbs" class="left"><a title="Results Summary"
href="Default.aspx? _act=VitalSearchR ...... <<INFORMATION I NEED>> ...
to view the entire record.</p></div><script xmlns:msxsl="urn:schemas-microsoft-com:xslt">
jQuery(document).ready(function() {
jQuery(".ancestry-information-tooltip").actooltip({
href: "#AncestryInformationTooltip", orientation: "bottomleft"});
});
所以我的问题是:如何获取由 JavaScript 生成的信息?
6 个回答
1
我遇到了同样的问题,最后通过设置期望的能力(desired_capabilities)解决了。
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
proxy = Proxy(
{
'proxyType': ProxyType.MANUAL,
'httpProxy': 'ip_or_host:port'
}
)
desired_capabilities = webdriver.DesiredCapabilities.PHANTOMJS.copy()
proxy.add_to_capabilities(desired_capabilities)
driver = webdriver.PhantomJS(desired_capabilities=desired_capabilities)
driver.get('test_url')
print driver.page_source
3
我觉得你在获取网页源代码的时候,JavaScript 还没有把动态的 HTML 渲染出来。
你可以先试着在导航和获取页面源代码之间加几秒钟的等待时间。
如果这样有效,那你就可以换一种不同的等待方式。
4
我也遇到过从网上获取JavaScript源代码的问题,我是按照上面Victory的建议解决的。
*第一步: 使用 execute_script
driver=webdriver.Chrome()
driver.get(urls)
innerHTML = driver.execute_script("return document.body.innerHTML")
#print(driver.page_source)
*第二步: 用 beautifulsoup
解析HTML(你可以通过pip命令下载 beautifulsoup
)
import bs4 #import beautifulsoup
import re
from time import sleep
sleep(1) #wait one second
root=bs4.BeautifulSoup(innerHTML,"lxml") #parse HTML using beautifulsoup
viewcount=root.find_all("span",attrs={'class':'short-view-count style-scope yt-view-count-renderer'}) #find the value which you need.
*第三步: 打印出你需要的值
for span in viewcount:
print(span.string)
*完整代码
from selenium import webdriver
import lxml
urls="http://www.archives.com/member/Default.aspx?_act=VitalSearchResult&lastName=Smith&state=UT&country=US&deathYear=2004&deathYearSpan=10&location=UT&activityID=9b79d578-b2a7-4665-9021-b104999cf031&RecordType=2"
driver = webdriver.PhantomJS()
##driver=webdriver.Chrome()
driver.get(urls)
innerHTML = driver.execute_script("return document.body.innerHTML")
##print(driver.page_source)
import bs4
import re
from time import sleep
sleep(1)
root=bs4.BeautifulSoup(innerHTML,"lxml")
viewcount=root.find_all("span",attrs={'class':'short-view-count style-scope yt-view-count-renderer'})
for span in viewcount:
print(span.string)
driver.quit()
13
其实你不需要使用那个变通的方法,你可以直接用下面这个:
driver = webdriver.PhantomJS()
driver.get('http://www.google.com/')
html = driver.find_element_by_tag_name('html').get_attribute('innerHTML')
51
你需要通过 javascript
来获取文档,可以使用 Selenium 的 execute_script
函数。
from time import sleep # this should go at the top of the file
sleep(5)
html = driver.execute_script("return document.getElementsByTagName('html')[0].innerHTML")
print html
这样就能获取到 <html>
标签里面的所有内容。