使用Beautiful Soup4可以获得<a> embedded text?</a>

时间:2013-01-04 10:48:48

标签: python-2.7 beautifulsoup

我正在使用带有Python的Beautiful Soup4,现在我已经达到了以下任何方式。所以现在以什么方式我可以获得价值狗,猫,马和Ids。请帮忙!

from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc)
soup.find_all('a')

# [<a class="Animal" href="http://example.com/elsie" id="link1">Dog</a>,
#  <a class="Animal" href="http://example.com/lacie" id="link2">Cat</a>,
#  <a class="Animal" href="http://example.com/tillie" id="link3">Horse</a>]

1 个答案:

答案 0 :(得分:2)

Documentation

for a in soup.find_all('a'):
    id = a.get('id')
    value = a.string # This is a NavigableString
    unicode_value = unicode(value)