我在一个网页上运行查询,然后得到结果URL。如果我右键单击查看html源代码,则可以看到JS生成的html代码。如果我仅使用urllib,则python无法获取JS代码。所以我看到了一些使用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!!
这是我在右键单击窗口中需要的源代码,(我需要信息部分)
</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"}); });
===========所以我的问题是===============如何获取JS生成的信息?
您将需要通过javascript使用硒execute_script功能来获取文档
javascript
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>标签内
<html>