DJ-coding
DJ-coding

Reputation: 323

How to extract value from specific P-tag with BeautifulSoup?

Is there a way to only extract the value for Acid(5.9 g/L) and Alcohol(14.5%)?

I thought of using find_all('p'), but it is giving me all the p tag while I only need two of them.

enter image description here

Upvotes: 0

Views: 249

Answers (1)

HedgeHog
HedgeHog

Reputation: 25196

Select the <h3> by its content and from there its direct sibling:

soup.select_one('h3:-soup-contains("Acid") + p').text

You could adapt this also for other elements if they are known otherwise you have to select all and check content against list

l = ['Acid','...']
for e in soup.select('.wine-specs p'):
    if e.text in l:
        print(e.text)

Upvotes: 1

Related Questions