Last active
November 28, 2019 13:37
-
-
Save mostafa1972/762377d883affff70fa04f962090514e to your computer and use it in GitHub Desktop.
Flipkart Web Scraping in Python: This will be store IPhone Mobile Name ,Price & Rating in csv file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#from selenium import webdriver | |
from bs4 import BeautifulSoup as soup | |
from urllib.request import urlopen as uReq | |
#import pandas as pd | |
my_url= 'https://www.flipkart.com/search?q=iphone' | |
uClient=uReq(my_url) | |
page_html=uClient.read() | |
uClient.close() | |
page_soup=soup(page_html,'html.parser') | |
containers=page_soup.findAll('div',{'class':'_1UoZlX'}) #'_3wU53n' | |
#print(len(containers)) | |
#print(soup.prettify(containers[5])) | |
container=containers[0] | |
prices=container.findAll('div',{'class':'col col-5-12 _2o7WAb'}) | |
#print(soup.prettify(prices[0])) | |
ratings=container.findAll('div',{'class':'niH0FQ'}) | |
filename='product.csv' | |
f=open(filename,'w') | |
headers='Product_Name,Pricing,Rating \n' | |
f.write(headers) | |
for container in containers: | |
product_name=container.div.img['alt'] | |
price_container=container.findAll('div',{'class':'col col-5-12 _2o7WAb'}) | |
price=price_container[0].text.strip() | |
rating_container = container.findAll('div',{'class':'hGSR34'}) | |
rating = rating_container[0].text.strip() | |
#string parsing | |
trim_price=''.join(price.split(',')) | |
rm_rupee=trim_price.split('₹') | |
add_rs_price='Rs.'+ rm_rupee[1] | |
split_price1=add_rs_price.split('N') | |
#split_price2 = split_price1.split('No') | |
final_price=split_price1[0] | |
split_rating=rating.split(' ') | |
final_rating=split_rating[0] | |
print(product_name.replace(',','|') + ',' + final_price + ',' + final_rating + '\n') | |
f.write(product_name.replace(',','|') + ',' + final_price + ',' + final_rating + '\n') | |
f.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment