the_t_test_1 the_t_test_1 - 1 year ago 69
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 Source

I think you want to use:

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

Or you could do:

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download