Chitransh Mathur
Chitransh Mathur

Reputation: 37

Web scraping price and names of mobiles?

I am a new learner of python and i am trying to scrape the name and price of particular mobiles form flipkart.

from requests import get
from bs4 import BeautifulSoup
import pandas as pd

name=[]
price=[]
rating=[]


url="https://www.flipkart.com/search?sid=tyy%2C4io&otracker=CLP_Filters&p%5B%5D=facets.ram%255B%255D%3D4%2BGB&p%5B%5D=facets.rating%255B%255D%3D4%25E2%2598%2585%2B%2526%2Babove"

results=requests.get(url)

soup=BeautifulSoup(results.text,"html.parser")

soup.prettify()

details=soup.find_all("div",attrs={"class":"bhgxx2 col-12-12"})

for mobiles in details:
    mob_name=mobiles.find("div",attrs={"class":"_3wU53n"})
    name.append(mob_name.text)
    print(name)

Output:

File "C:/Users/hp/Desktop/mobile scrapping.py", line 30, in name.append(mob_name.text)

AttributeError: 'NoneType' object has no attribute 'text'

Upvotes: 0

Views: 534

Answers (1)

Karthik
Karthik

Reputation: 2431

Please Check this out

import requests
from bs4 import BeautifulSoup
url="https://www.flipkart.com/search?sid=tyy%2C4io&otracker=CLP_Filters&p%5B%5D=facets.ram%255B%255D%3D4%2BGB&p%5B%5D=facets.rating%255B%255D%3D4%25E2%2598%2585%2B%2526%2Babove"
results=requests.get(url)
soup=BeautifulSoup(results.text,"html.parser")
mobiles=[]
rates=[]
details=soup.findAll("div",attrs={"class":"_3wU53n"})
for i in details:
    mobiles.append(i.text)
prices=soup.findAll("div",attrs={"class":"_1vC4OE _2rQ-NK"})
for j in prices:
    rates.append(j.text)
final=[[x,y] for x,y in zip(mobiles,rates)]
print(final)

Output: ['Realme Narzo 10A (So White, 64 GB)', '₹9,999'], ['Realme Narzo 10 (That Blue, 128 GB)', '₹11,999']]

Upvotes: 2

Related Questions