【问题标题】:FInd javascript-links with Python使用 Python 查找 javascript 链接
【发布时间】:2014-08-02 20:38:58
【问题描述】:
【问题讨论】:
标签:
python
beautifulsoup
mechanize
【解决方案1】:
只需要 BeautifulSoup 这很容易:
js_links = soup.select('a[href^="javascript:"]')
这会选择所有具有href 属性且值以javascript: 开头的<a> 元素:
>>> 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