<meta itemprop="streetAddress" content="4103 Beach Bluff Rd">
I have to get the content '4103 Beach Bluff Rd'. I'm trying to get this done with BeautifulSoup
so, I'm trying this:
soup = BeautifulSoup('<meta itemprop="streetAddress" content="4103 Beach Bluff Rd"> ')
soup.find(itemprop="streetAddress").get_text()
but I'm getting an empy string as result, which may have sense given that when a print the soup object
print soup
I get the this:
<html><head><meta content="4103 Beach Bluff Rd" itemprop="streetAddress"/> </head></html>
Apparently the data I want is in the 'meta content' tag, how can I get this data?
soup.find(itemprop="streetAddress").get_text()
You are getting the text of a matched element. Instead, get the "content" attribute value:
soup.find(itemprop="streetAddress").get("content")
This is possible since BeautifulSoup
provides a dictionary-like interface to tag attributes:
You can access a tag’s attributes by treating the tag like a dictionary.
Demo:
>>> from bs4 import BeautifulSoup
>>>
>>> soup = BeautifulSoup('<meta itemprop="streetAddress" content="4103 Beach Bluff Rd"> ')
>>> soup.find(itemprop="streetAddress").get_text()
u''
>>> soup.find(itemprop="streetAddress").get("content")
'4103 Beach Bluff Rd'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With