Web Scrapping of flipkart website

Dear team,

I want to scrap the flipkart website to fetch the product name, price and ratings. The page i want to scrap contain 24 phones but when i run my code i get one phone item of the 24 phones items. Apparently my code is working very well.
‘’’
import requests
from bs4 import BeautifulSoup
response = requests.get(‘Samsung- Buy Products Online at Best Price in India - All Categories | Flipkart.com’)

    content = response.content
    parser = BeautifulSoup(content, 'html.parser')
    containers = parser.find_all('div', class_ = '_3O0U0u')

    container = containers[0]
    print(container.div.img['alt'])

    price = parser.find_all('div', class_='_1vC4OE _2rQ-NK')
    print(price[0].text)

    ratings = parser.find_all('div', class_='hGSR34')
    print(ratings[0].text)

    for container in containers:
        product_name = container.div.img['alt']
        
        price_container = parser.find_all('div', class_='_1vC4OE _2rQ-NK')
        price = price_container[0].text
        
        rating_container = parser.find_all('div', class_='hGSR34')
        rating = rating_container[0].text

    print('product_name: '  + product_name)
    print('price: '  + price)
    print('ratings: '+  rating)'''

What am I doing wrong? How can I modify my code to print the product name, price and rating of the 24 phone in one of the pages

Thanks

Hi @ignatiusebigwai

I saw your code, you are doing the mistake in parsing the text using BeautifulSoup. You have to parse response.text instead of response.content.

Here is the correct code for the first element of containers.

import requests
from bs4 import BeautifulSoup
response = requests.get('https://www.flipkart.com/search?q=samsung&otracker=search&otracker1=search&marketplace=FLIPKART&as-show=on&as=off&as-pos=0&as-type=HISTORY&page=2')

parser = BeautifulSoup(response.text, 'html.parser')
containers = parser.find_all('div', class_ = '_3O0U0u')

container = containers[0]
print(container.div.img['alt'])

price = parser.find_all('div', class_='_1vC4OE _2rQ-NK')
print(price[0].text)

ratings = parser.find_all('div', class_='hGSR34')
print(ratings[0].text)

You can see my code also, I wrote it for only one page you can edit it for other pages.

Flipkart_Mobile_Data.ipynb (15.9 KB)

I used your given URL and here is the dataset for Page 2, which is given in the URL.

mobile_data.csv (2.2 KB)

I hope this helps :slight_smile:

Click here to view the jupyter notebook file in a new tab

2 Likes

Hi Premchandra,

Thanks very much for your response.
Your code worked. I went ahead to read the Beautiful Soup documentation. I thought it was my for loop that wasn’t working correcting.

Thanks once again,
Ignatius.

1 Like