Home > Back-end >  how to find an existing number in an specific tag, but this tag is existing more than one time (beau
how to find an existing number in an specific tag, but this tag is existing more than one time (beau

Time:10-31

I want to find the value 280 that is in the bdi tag. I've tried to track it by using the findAll method but it gives me another existing number in another bdi tag

HTML Format

img

Code

def function():
    price=soup.findAll('bdi')[4].get_text()
    return price

Output

199
340

CodePudding user response:

First find main HTML tag which containt that bdi price tag and based on that i have used find_all method on div tag and from that on index 0 data is present so we can fetch according to it!

import requests
from bs4 import BeautifulSoup
response = requests.get('https://www.maroc4gaming.net/index.php/produit/erxung-j5-blue/')
soup = BeautifulSoup(response.text,'html.parser')

Here in the code i have used list compherension and for loop in alternative condtion

main_data=soup.find_all("div",class_="price-wrapper")[0].find_all("span",class_="woocommerce-Price-amount amount")
price_len=len(main_data)

if price_len==1:
    price=[i.text.split("\xa0")[0] for i in main_data]
    print("Latest Price"   " "  price[0])
else:
    price=[i.text.split("\xa0")[0] for i in main_data]
    print("Latest Price"   " "  price[1])

Output:

'280.0'
  • Related