skip to Main Content

I am scraping an product information. But I scrape its price it doesn’t give me proper output. There is no error but not the desired output.

And also it produce error while finding the category of a product.
Here is my code.

import requests
from bs4 import BeautifulSoup as bs
import pandas

url='https://shop.eziline.com/product/uncategorized/umrah-management-system/'
r=requests.get(url)
soup=bs(r.content,'html.parser')

name=soup.find(class_='product_title entry-title').text.strip()
print(name)
price=soup.find('span',class_='woocommerce-Price-amount amount').text.strip()
print(price)
detail=soup.find(class_='woo-product-details_short-description').text.strip()
print(detail)
category=soup.find('cats-link a').text.strip()
print(category)

2

Answers


  1. The attributes you are using in the find method apply to more than one tag, you can view all the tags using findAll as follows:

    for t in soup.findAll('span',class_='woocommerce-Price-amount amount'):
        print(str(t) + "n")
    

    which will result in the following output

    <span class="woocommerce-Price-amount amount"><bdi>0.00<span class="woocommerce-Price-currencySymbol">$</span></bdi></span>
    
    <span class="woocommerce-Price-amount amount"><bdi>0.00<span class="woocommerce-Price-currencySymbol">$</span></bdi></span>
    
    <span class="woocommerce-Price-amount amount">350.00<span class="woocommerce-Price-currencySymbol">$</span></span>
    
    <span class="woocommerce-Price-amount amount"><bdi>850.00<span class="woocommerce-Price-currencySymbol">$</span></bdi></span>
    

    in your code the find method returns the first occurrence of a span with classes woocommerce-Price-amount amount, and hence the output is 0.00
    enter image description here

    to get only the last tag which is the price you can use

    price = soup.findAll('span',class_='woocommerce-Price-amount amount')[-1].text.strip()
    
    Login or Signup to reply.
  2. You are

    1. picking up the first price rather than the target price. Instead, you can use the main content id as an anchor to the correct section to return the price from

    2. You are trying to use css selector syntax in the last line without applying via the appropriate method and adding in the syntax for a class selector. You could also use category=soup.find(class_='cats-link').a.text.strip()

    Instead:

    import requests
    from bs4 import BeautifulSoup as bs
    
    url='https://shop.eziline.com/product/uncategorized/umrah-management-system/'
    r=requests.get(url)
    soup=bs(r.content,'lxml')
    name=soup.find(class_='product_title entry-title').text.strip()
    print(name)
    price=soup.select_one('#main-content .woocommerce-Price-amount bdi').text.strip()
    print(price)
    detail=soup.find(class_='woo-product-details_short-description').text.strip()
    print(detail)
    category=soup.select_one('.cats-link a').text.strip()
    print(category)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search