python - bs4只抓取某些链接

时间:2014-10-10 02:47:58

标签: python beautifulsoup

<a title="dynamic link" href="test.php">text</a>

如何才能抓住唯一具有上述示例标题标签的内容?

1 个答案:

答案 0 :(得分:1)

You can use select method with commonly-used css selector

>>> from bs4 import BeautifulSoup
>>> html = '''
... <html>
...     <body>
...         <a title="dynamic link" href="test1.php">text</a>
...         <a href="test2.php">text</a>
...     </body>
... </html>
... '''
>>> soup = BeautifulSoup(html)
>>> soup.select('a[title]')
[<a href="test1.php" title="dynamic link">text</a>]