与Python的javascript链接

时间:2014-08-02 20:38:58

标签: python beautifulsoup mechanize

有没有办法在python的网页上找到javascript-links?我使用机械化,但我无法找到我想要的所有链接。我想在此网站上的图片上添加网址:http://500px.com/popular

1 个答案:

答案 0 :(得分:1)

只需使用BeautifulSoup,这很简单:

js_links = soup.select('a[href^="javascript:"]')

这将选择<a>属性的所有href元素,其值以javascript:开头:

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('''\
... <html><body>
... <a href="http://stackoverflow.com">Not a javascript link</a>
... <a name="target">Not a link, no href</a>
... <a href="javascript:alert('P4wned');">Javascript link (with scary message)</a>
... <a href="javascript:return False">Another javascript link</a>
... </body></html>
... ''')
>>> for link in soup.select('a[href^="javascript:"]'):
...     print link['href'], link.get_text()
... 
javascript:alert('P4wned'); Javascript link (with scary message)
javascript:return False Another javascript link