Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beautiful soup meta content tag

<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?

like image 354
Luis Ramon Ramirez Rodriguez Avatar asked Dec 14 '22 10:12

Luis Ramon Ramirez Rodriguez


1 Answers

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'
like image 167
alecxe Avatar answered Jan 04 '23 09:01

alecxe