While I try to scrape the items individually I get the results, but when I use the try except method I don't get the results.
from bs4 import BeautifulSoup as soup
import pandas as pd
import requests
import urllib
import requests, random
data =[]
url = 'https://www.flipkart.com/search?q=iphone'
def getdata (url):
user_agents = [
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:77.0) Gecko/20100101 Firefox/77.0',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'
]
user_agent = random.choice(user_agents)
header_ = {'User-Agent': user_agent}
req = urllib.request.Request(url, headers=header_)
amazon_html = urllib.request.urlopen(req).read()
f_soup = soup(amazon_html,'html.parser')
for e in f_soup.select('div[data-tkid="2f9a5ad9-8f64-4327-8a23-5069ba20a68b.MOBFWBYZBZ7Y56WD.SEARCH"]'):
try:
title = e.find('div',{'class':'_4rR01T'}).text
except:
title = None
try:
rating = e.find('span',{'class':'a-size-base s-underline-text'}).text
except:
rating = 0
data.append({
'Title':title,
'Rating':rating
})
return data
getdata (url)
OUTPUT
[]
output = pd.DataFrame(data)
output
WEBSITE LINK : https://www.flipkart.com/search?q=iphone&page=1