the_t_test_1
the_t_test_1

Reputation: 1263

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

Upvotes: 0

Views: 1469

Answers (1)

Pythonista
Pythonista

Reputation: 11645

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

Upvotes: 2

Related Questions