the_t_test_1 the_t_test_1 - 7 months ago 10
Python Question

Get address text from item tag with BeautifulSoup and Python

I am fine with extracting tag info but trying to locate text is proving tricky.

I want to get the address from this:

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


I've tried:

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


But I get:

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


Any help with this simple problem much appreciated... I've followed tutorials and they all seem to indicate this should work :S

Answer

I think you want to use:

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

Or you could do:

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

Example:

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)

Result:

Some Road, London SW1 1AA
Comments