Andy
Andy

Reputation: 373

Get data that's in a tag (HTML content)

<meta itemprop="price" content="4.05"/>

Here's my HTML that I need to extract 4.05 from.

I'm using BeautifulSoup with Python.

EDIT:

I also need to use itemprop="price" because I have more than one <meta content="x"/>

soup.find("meta", {"itemprop":"price"})["content"]
4.05

Upvotes: 1

Views: 214

Answers (2)

Tamim Shahriar
Tamim Shahriar

Reputation: 739

>>> from bs4 import BeautifulSoup
>>> text = '<meta itemprop="price" content="4.05"/>'
>>> soup = BeautifulSoup(text)
>>> soup
<html><head><meta content="4.05" itemprop="price"/></head></html>
>>> soup.meta
<meta content="4.05" itemprop="price"/>
>>> soup.meta["content"]
'4.05'
>>> 

Upvotes: 1

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

html = '<meta itemprop="price" content="4.05"/>'
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)

soup.find("meta")["content"]
4.05
soup.meta["content"]
4.05

Upvotes: 2

Related Questions