【发布时间】:2014-05-09 11:45:29
【问题描述】:
我在一个网页中运行查询,然后得到结果 url。如果我右键查看 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!!
这是我在右键窗口中需要的源代码,(我想要 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"});
});
所以我的问题是:如何获取JS生成的信息?
【问题讨论】:
-
你想要的html代码在页面上是什么样子的?您将希望使用 selenium 的
get_element_by_*函数之一,但具体如何取决于 html 本身。 -
我的意思是一切。例如,您在 google 中输入一些内容。在结果网页中,右键单击,查看源代码。这就是我想要的“一切”。
标签: javascript python selenium