使用BeautifulSoup和Python从item标签中获取地址文本

时间:2016-04-21 12:12:08

标签: python web-scraping beautifulsoup

我很好地提取标签信息,但试图找到文本证明是棘手的。

我想从这里得到地址:

<item itemprop="streetAddress">Some Road, London SW1 1AA</item>

我试过了:

address = soup.find("item", "streetAddress").text
print address

但我明白了:

    address = soup.find("item", "streetAddress").text
AttributeError: 'NoneType' object has no attribute 'text'

对这个简单问题的任何帮助都非常感谢...我已经按照教程进行了操作,他们似乎都表明这应该有效:S

1 个答案:

答案 0 :(得分:2)

我想你想用:

address = soup.find('item', {'itemprop': 'streetAddress').text

或者你可以这样做:

address = soup.find('item', itemprop = 'streetAddress').text

示例:

from bs4 import BeautifulSoup as BS
html = "<item itemprop='streetAddress'>Some Road, London SW1 1AA</item>"
soup = BS(html, 'html.parser')
print(soup.find('item', {'itemprop':'streetAddress'}).text)

结果:

Some Road, London SW1 1AA