打开由Javascript处理的特定“#”链接?

时间:2013-06-30 21:47:40

标签: javascript python url

我希望在Python中访问一个由JavaScript处理的链接,如下所示:

<a href="#" class="example"> Hello World </a>

我想根据属性或<a>元素的 id 属性找到HTML字符串中的链接。

是否可以在Python中执行此操作?

1 个答案:

答案 0 :(得分:1)

你不能,因为这是一个自引用链接。您已打开文档。

网址中的#表示文档中的位置。当网址#启动时,它位于当前文档中的位置;浏览器将滚动到#之后命名的任何ID。在以下示例中,单击<a href="#footer">链接指示浏览器滚动文档以将<div id="footer">元素放置在浏览器窗口的顶部:

<a href="#footer">to the end of the document</a>

<!-- long document follows -->

<div id="footer">Something at the bottom of the document</div>

如果网址只包含# ,则该网址为无操作。它是一个占位符,用于JavaScript通常拦截链接点击。使用Python处理此文档时,您可以完全忽略它。您的Python HTML解析器不是浏览器,也没有运行JavaScript来处理该链接元素上的鼠标点击。甚至没有鼠标点击。

如果 试图处理JavaScript驱动的页面,您可以使用JavaScript调试器(大多数浏览器附带)来弄清楚它在做什么,或者运行由蟒蛇。您可以使用Ghost.py来执行后者:

from ghost import Ghost
ghost = Ghost()
page, extra_resources = ghost.open("http://jeanphi.fr")
assert page.http_status==200 and 'jeanphix' in ghost.content

这是一个无头的Webkit浏览器。