Mac*_*nhe 26 javascript python selenium
我在一个网页上运行查询,然后我得到结果网址.如果我右键单击查看html源代码,我可以看到JS生成的html代码.如果我只是使用urllib,python就无法获取JS代码.所以我看到了一些使用硒的解决方案.这是我的代码:
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!!
Run Code Online (Sandbox Code Playgroud)
这是我在右键单击窗口中需要的源代码,(我想要信息部分)
</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"});
});
Run Code Online (Sandbox Code Playgroud)
===========所以我的问题是===============如何获取JS生成的信息?
Vic*_*ory 28
您需要通过javascriptseleniums 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
Run Code Online (Sandbox Code Playgroud)
这将获得<html>标签内的所有内容
没有必要使用该变通方法,您可以使用:
driver = webdriver.PhantomJS()
driver.get('http://www.google.com/')
html = driver.find_element_by_tag_name('html').get_attribute('innerHTML')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33713 次 |
| 最近记录: |