0% found this document useful (0 votes)
2 views

webdatatopandas

The document outlines a Python script that scrapes laptop data from Flipkart using BeautifulSoup and requests. It extracts laptop names and prices, storing them in a pandas DataFrame for further analysis. The script successfully retrieves product information, including specifications and pricing details.

Uploaded by

xiaochuuyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

webdatatopandas

The document outlines a Python script that scrapes laptop data from Flipkart using BeautifulSoup and requests. It extracts laptop names and prices, storing them in a pandas DataFrame for further analysis. The script successfully retrieves product information, including specifications and pricing details.

Uploaded by

xiaochuuyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

webdatatopandas

January 26, 2025

[16]: from bs4 import BeautifulSoup


import requests
import pandas as pd
url="https://www.flipkart.com/search?
↪q=laptops&otracker=search&otracker1=search&marketplace=FLIPKART&as-show=on&as=off"

h=({'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML,␣


↪like Gecko) Chrome/131.0.0.0 Safari/537.36','Accept-Language':'en-us'})

r=requests.get(url,headers=h)
r

[16]: <Response [200]>

[18]: soup=BeautifulSoup(r.content,"html.parser")
names=soup.find_all("div",class_="KzDlHZ")
names

[18]: [<div class="KzDlHZ">Acer Aspire 7 Intel Core i5 13th Gen 13420H - (16 GB/512 GB
SSD/Windows 11 Home/4 GB Graphics/NVIDIA G…</div>,
<div class="KzDlHZ">Lenovo IdeaPad Slim 3 Intel Core i5 12th Gen 12450H - (8
GB/512 GB SSD/Windows 11 Home) 14IAH8 Thin an…</div>,
<div class="KzDlHZ">HP Intel Core i5 12th Gen 1235U - (16 GB/512 GB SSD/Windows
11 Home) 15s-fq5330TU|15-fd0111TU Thin and…</div>,
<div class="KzDlHZ">HP Pavilion Intel Core i5 12th Gen 1235U - (16 GB/512 GB
SSD/Windows 11 Home) 14-dv2014TU Thin and Lig…</div>,
<div class="KzDlHZ">ASUS Vivobook Go 15 AMD Ryzen 5 Quad Core 7520U - (8 GB/512
GB SSD/Windows 11 Home) E1504FA-NJ521WS Th…</div>,
<div class="KzDlHZ">Lenovo LOQ Intel Core i5 12th Gen 12450HX - (12 GB/512 GB
SSD/Windows 11 Home/4 GB Graphics/NVIDIA GeF…</div>,
<div class="KzDlHZ">ASUS ExpertBook B14 Intel Core i3 12th Gen 1215U - (8
GB/512 GB SSD/Windows 11 Home) B1402CBA-NK1494WS…</div>,
<div class="KzDlHZ">ASUS Vivobook Go 15 AMD Ryzen 3 Quad Core 7320U - (8 GB/512
GB SSD/Windows 11 Home) E1504FA-NJ321WS Th…</div>,
<div class="KzDlHZ">ASUS Vivobook 15 Intel Core i5 12th Gen 1235U - (8 GB/512
GB SSD/Windows 11 Home) X1504ZA-NJ527WS|X150…</div>,
<div class="KzDlHZ">HP Intel Core i3 12th Gen 1215U - (16 GB/512 GB SSD/Windows
11 Home) 15s-fy5004TU Thin and Light Lapto…</div>,
<div class="KzDlHZ">Lenovo LOQ Intel Core i5 12th Gen 12450HX - (12 GB/512 GB
SSD/Windows 11 Home/4 GB Graphics/NVIDIA GeF…</div>,

1
<div class="KzDlHZ">Lenovo IdeaPad Slim 3 Intel Core i3 12th Gen 1215U - (8
GB/512 GB SSD/Windows 11 Home) 15IAU7 Thin and…</div>,
<div class="KzDlHZ">Lenovo V14 Intel Core i5 12th Gen 1235U - (16 GB/512 GB
SSD/Windows 11 Home) V 14 Thin and Light Lapto…</div>,
<div class="KzDlHZ">ASUS Vivobook 15, with Backlit Keyboard, Intel Core i5 12th
Gen 1235U - (16 GB/512 GB SSD/Windows 11 H…</div>,
<div class="KzDlHZ">ASUS Vivobook 15 Intel Core i3 12th Gen 1215U - (16 GB/512
GB SSD/Windows 11 Home) X1504ZA-NJ342WS Thi…</div>,
<div class="KzDlHZ">HP 15s Intel Core i3 12th Gen 1215U - (8 GB/512 GB
SSD/Windows 11 Home) 15s-fy5003TU Thin and Light La…</div>,
<div class="KzDlHZ">HP Victus 15 Intel Core i5 13th Gen 13420H - (16 GB/512 GB
SSD/Windows 11 Home/6 GB Graphics/NVIDIA Ge…</div>,
<div class="KzDlHZ">ASUS TUF Gaming F15, 90WHrs Battery Intel Core i7 13th Gen
13620H - (16 GB/512 GB SSD/Windows 11 Home/…</div>,
<div class="KzDlHZ">ASUS Vivobook 15 Intel Core i3 12th Gen 1215U - (8 GB/512
GB SSD/Windows 11 Home) X1504ZA-NJ322WS Thin…</div>,
<div class="KzDlHZ">Acer Aspire 3 Intel Core i3 13th Gen 1305U - (8 GB/512 GB
SSD/Windows 11 Home) A324-53 Thin and Light …</div>,
<div class="KzDlHZ">HP Victus Intel Core i5 12th Gen 12450H - (8 GB/512 GB
SSD/Windows 11 Home/4 GB Graphics/NVIDIA GeForc…</div>,
<div class="KzDlHZ">Lenovo IdeaPad Slim 3 Intel Core i5 12th Gen 12450H - (8
GB/512 GB SSD/Windows 11 Home) 15IAH8 Thin an…</div>,
<div class="KzDlHZ">HP (15s-fq5007TU) Intel Core i3 12th Gen 1215U - (8 GB/512
GB SSD/Windows 11 Home) 15s-fq5327TU Thin a…</div>,
<div class="KzDlHZ">Acer Aspire 3 with Backlit Keyboard, Intel Core i5 12th Gen
1235U - (16 GB/1 TB SSD/Windows 11 Home) A…</div>]

[20]: nam=[]
for i in names:
nam.append(i.string)
nam

[20]: ['Acer Aspire 7 Intel Core i5 13th Gen 13420H - (16 GB/512 GB SSD/Windows 11
Home/4 GB Graphics/NVIDIA G…',
'Lenovo IdeaPad Slim 3 Intel Core i5 12th Gen 12450H - (8 GB/512 GB SSD/Windows
11 Home) 14IAH8 Thin an…',
'HP Intel Core i5 12th Gen 1235U - (16 GB/512 GB SSD/Windows 11 Home)
15s-fq5330TU|15-fd0111TU Thin and…',
'HP Pavilion Intel Core i5 12th Gen 1235U - (16 GB/512 GB SSD/Windows 11 Home)
14-dv2014TU Thin and Lig…',
'ASUS Vivobook Go 15 AMD Ryzen 5 Quad Core 7520U - (8 GB/512 GB SSD/Windows 11
Home) E1504FA-NJ521WS Th…',
'Lenovo LOQ Intel Core i5 12th Gen 12450HX - (12 GB/512 GB SSD/Windows 11
Home/4 GB Graphics/NVIDIA GeF…',
'ASUS ExpertBook B14 Intel Core i3 12th Gen 1215U - (8 GB/512 GB SSD/Windows 11
Home) B1402CBA-NK1494WS…',
'ASUS Vivobook Go 15 AMD Ryzen 3 Quad Core 7320U - (8 GB/512 GB SSD/Windows 11

2
Home) E1504FA-NJ321WS Th…',
'ASUS Vivobook 15 Intel Core i5 12th Gen 1235U - (8 GB/512 GB SSD/Windows 11
Home) X1504ZA-NJ527WS|X150…',
'HP Intel Core i3 12th Gen 1215U - (16 GB/512 GB SSD/Windows 11 Home)
15s-fy5004TU Thin and Light Lapto…',
'Lenovo LOQ Intel Core i5 12th Gen 12450HX - (12 GB/512 GB SSD/Windows 11
Home/4 GB Graphics/NVIDIA GeF…',
'Lenovo IdeaPad Slim 3 Intel Core i3 12th Gen 1215U - (8 GB/512 GB SSD/Windows
11 Home) 15IAU7 Thin and…',
'Lenovo V14 Intel Core i5 12th Gen 1235U - (16 GB/512 GB SSD/Windows 11 Home) V
14 Thin and Light Lapto…',
'ASUS Vivobook 15, with Backlit Keyboard, Intel Core i5 12th Gen 1235U - (16
GB/512 GB SSD/Windows 11 H…',
'ASUS Vivobook 15 Intel Core i3 12th Gen 1215U - (16 GB/512 GB SSD/Windows 11
Home) X1504ZA-NJ342WS Thi…',
'HP 15s Intel Core i3 12th Gen 1215U - (8 GB/512 GB SSD/Windows 11 Home)
15s-fy5003TU Thin and Light La…',
'HP Victus 15 Intel Core i5 13th Gen 13420H - (16 GB/512 GB SSD/Windows 11
Home/6 GB Graphics/NVIDIA Ge…',
'ASUS TUF Gaming F15, 90WHrs Battery Intel Core i7 13th Gen 13620H - (16 GB/512
GB SSD/Windows 11 Home/…',
'ASUS Vivobook 15 Intel Core i3 12th Gen 1215U - (8 GB/512 GB SSD/Windows 11
Home) X1504ZA-NJ322WS Thin…',
'Acer Aspire 3 Intel Core i3 13th Gen 1305U - (8 GB/512 GB SSD/Windows 11 Home)
A324-53 Thin and Light …',
'HP Victus Intel Core i5 12th Gen 12450H - (8 GB/512 GB SSD/Windows 11 Home/4
GB Graphics/NVIDIA GeForc…',
'Lenovo IdeaPad Slim 3 Intel Core i5 12th Gen 12450H - (8 GB/512 GB SSD/Windows
11 Home) 15IAH8 Thin an…',
'HP (15s-fq5007TU) Intel Core i3 12th Gen 1215U - (8 GB/512 GB SSD/Windows 11
Home) 15s-fq5327TU Thin a…',
'Acer Aspire 3 with Backlit Keyboard, Intel Core i5 12th Gen 1235U - (16 GB/1
TB SSD/Windows 11 Home) A…']

[21]: prices=soup.find_all("div",class_="Nx9bqj _4b5DiR")


prices

[21]: [<div class="Nx9bqj _4b5DiR">�53,990</div>,


<div class="Nx9bqj _4b5DiR">�49,280</div>,
<div class="Nx9bqj _4b5DiR">�47,990</div>,
<div class="Nx9bqj _4b5DiR">�56,990</div>,
<div class="Nx9bqj _4b5DiR">�34,790</div>,
<div class="Nx9bqj _4b5DiR">�58,990</div>,
<div class="Nx9bqj _4b5DiR">�32,990</div>,
<div class="Nx9bqj _4b5DiR">�29,885</div>,
<div class="Nx9bqj _4b5DiR">�41,990</div>,
<div class="Nx9bqj _4b5DiR">�38,990</div>,

3
<div class="Nx9bqj _4b5DiR">�58,990</div>,
<div class="Nx9bqj _4b5DiR">�34,600</div>,
<div class="Nx9bqj _4b5DiR">�37,400</div>,
<div class="Nx9bqj _4b5DiR">�46,990</div>,
<div class="Nx9bqj _4b5DiR">�34,990</div>,
<div class="Nx9bqj _4b5DiR">�34,790</div>,
<div class="Nx9bqj _4b5DiR">�74,990</div>,
<div class="Nx9bqj _4b5DiR">�96,990</div>,
<div class="Nx9bqj _4b5DiR">�31,990</div>,
<div class="Nx9bqj _4b5DiR">�29,990</div>,
<div class="Nx9bqj _4b5DiR">�57,990</div>,
<div class="Nx9bqj _4b5DiR">�51,390</div>,
<div class="Nx9bqj _4b5DiR">�38,990</div>,
<div class="Nx9bqj _4b5DiR">�44,990</div>]

[23]: pir=[]
for i in prices:
pir.append(i.string)
pir

[23]: ['�53,990',
'�49,280',
'�47,990',
'�56,990',
'�34,790',
'�58,990',
'�32,990',
'�29,885',
'�41,990',
'�38,990',
'�58,990',
'�34,600',
'�37,400',
'�46,990',
'�34,990',
'�34,790',
'�74,990',
'�96,990',
'�31,990',
'�29,990',
'�57,990',
'�51,390',
'�38,990',
'�44,990']

[25]: df=pd.DataFrame({"names":nam,"prices":pir})
df

4
[25]: names prices
0 Acer Aspire 7 Intel Core i5 13th Gen 13420H - … �53,990
1 Lenovo IdeaPad Slim 3 Intel Core i5 12th Gen 1… �49,280
2 HP Intel Core i5 12th Gen 1235U - (16 GB/512 G… �47,990
3 HP Pavilion Intel Core i5 12th Gen 1235U - (16… �56,990
4 ASUS Vivobook Go 15 AMD Ryzen 5 Quad Core 7520… �34,790
5 Lenovo LOQ Intel Core i5 12th Gen 12450HX - (1… �58,990
6 ASUS ExpertBook B14 Intel Core i3 12th Gen 121… �32,990
7 ASUS Vivobook Go 15 AMD Ryzen 3 Quad Core 7320… �29,885
8 ASUS Vivobook 15 Intel Core i5 12th Gen 1235U … �41,990
9 HP Intel Core i3 12th Gen 1215U - (16 GB/512 G… �38,990
10 Lenovo LOQ Intel Core i5 12th Gen 12450HX - (1… �58,990
11 Lenovo IdeaPad Slim 3 Intel Core i3 12th Gen 1… �34,600
12 Lenovo V14 Intel Core i5 12th Gen 1235U - (16 … �37,400
13 ASUS Vivobook 15, with Backlit Keyboard, Intel… �46,990
14 ASUS Vivobook 15 Intel Core i3 12th Gen 1215U … �34,990
15 HP 15s Intel Core i3 12th Gen 1215U - (8 GB/51… �34,790
16 HP Victus 15 Intel Core i5 13th Gen 13420H - (… �74,990
17 ASUS TUF Gaming F15, 90WHrs Battery Intel Core… �96,990
18 ASUS Vivobook 15 Intel Core i3 12th Gen 1215U … �31,990
19 Acer Aspire 3 Intel Core i3 13th Gen 1305U - (… �29,990
20 HP Victus Intel Core i5 12th Gen 12450H - (8 G… �57,990
21 Lenovo IdeaPad Slim 3 Intel Core i5 12th Gen 1… �51,390
22 HP (15s-fq5007TU) Intel Core i3 12th Gen 1215U… �38,990
23 Acer Aspire 3 with Backlit Keyboard, Intel Cor… �44,990

[27]: df.to_csv("scrapeddata.csv",index=False)

You might also like