如果锚文本包含关键字,则提取链接

时间:2014-06-06 20:53:49

标签: python web-scraping beautifulsoup

import BeautifulSoup

html = """
<html><head></head>
<body>
<a href='http://www.gurletins.com'>My HomePage</a>
<a href='http://www.gurletins.com/sections'>Sections</a>
</body>
</html>
"""

soup = BeautifulSoup.BeautifulSoup(html)

现在我想获取包含关键字Home

的链接

有人可以告诉我如何使用BeautifulSoup吗?

2 个答案:

答案 0 :(得分:2)

html = """
<html><head></head>
<body>
<a href='http://www.gurletins.com'>My HomePage</a>
<a href='http://www.gurletins.com/sections'>Sections</a>
</body>
</html>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)

for i in soup.find_all("a"):
    if "HOME" in str(i).split(">")[1].upper():
        print i["href"]
http://www.gurletins.com

答案 1 :(得分:1)

有一种更好的方法。在text参数中传递正则表达式:

import re
from bs4 import BeautifulSoup

html = """
<html><head></head>
<body>
<a href='http://www.gurletins.com'>My HomePage</a>
<a href='http://www.gurletins.com/sections'>Sections</a>
</body>
</html>
"""

soup = BeautifulSoup(html)
for a in soup.find_all("a", text=re.compile('Home')):
    print a['href']

打印:

http://www.gurletins.com

请注意,默认情况下它区分大小写。如果您需要使其不敏感,请将re.IGNORECASE标记传递给re.compile()

re.compile('Home', re.IGNORECASE)

演示:

>>> import re
>>> from bs4 import BeautifulSoup
>>> 
>>> html = """
... <html><head></head>
... <body>
... <a href='http://www.gurletins.com'>My HomePage</a>
... <a href='http://www.gurletins.com/sections'>Sections</a>
... <a href='http://www.gurletins.com/home'>So nice to be home</a>
... </body>
... </html>
... """
>>> 
>>> soup = BeautifulSoup(html)
>>> for a in soup.find_all("a", text=re.compile('Home', re.IGNORECASE)):
...     print a['href']
... 
http://www.gurletins.com
http://www.gurletins.com/home
相关问题