Home > Software design >  Beautiful soup returns random elements when called for a certain tag or class name
Beautiful soup returns random elements when called for a certain tag or class name

Time:02-12

So from the following site, I need to filter the current price of the product listed which is located in the tag

with the class name - current price, so I wrote the following code to get the result, but what I get is the price with some other bunch of stuffs, how to filter the price alone from the html code ?

https://www.tendercuts.in/chicken

here is the code i used :

   import requests
   from bs4 import BeautifulSoup

   baseurl = 'https://www.tendercuts.in/'

   r = requests.get('https://www.tendercuts.in/chicken')
   soup = BeautifulSoup(r.content, 'lxml')

   productweight = soup.find_all('p', class_='currentprice')

   print(productweight)

CodePudding user response:

You need to use the correct class as follows:

import requests
from bs4 import BeautifulSoup

r = requests.get('https://www.tendercuts.in/chicken')
soup = BeautifulSoup(r.content, 'lxml')

for p in soup.find_all('p', class_='current-price'):
    print(p.text)

CodePudding user response:

Here you go...there are two changes you have to make in your code. Check the following code and comment.

import requests
from bs4 import BeautifulSoup

baseurl = 'https://www.tendercuts.in/'

r = requests.get('https://www.tendercuts.in/chicken')
soup = BeautifulSoup(r.content, 'html.parser')

productweight = soup.find_all('p', class_='current-price')

print(productweight)
  • Related