Home > Back-end >  How to extract value from specific P-tag with BeautifulSoup?
How to extract value from specific P-tag with BeautifulSoup?

Time:12-18

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

CodePudding user response:

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)
  • Related