Last active
February 24, 2024 10:45
-
-
Save harsh546/a42d1176fca694dded0a8efd96eff19d to your computer and use it in GitHub Desktop.
Web Scrapring Price Comparison
This file contains 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
# Code by Harsh | |
# Python program that compares the price of a product on Amazon and Flipkart using web scraping. | |
""" | |
First, you need to install the requests and beautifulsoup4 libraries. | |
You can do this by running the following command in your terminal or command prompt: | |
pip install requests beautifulsoup4 | |
""" | |
import requests | |
from bs4 import BeautifulSoup | |
# Product name and URL on Amazon and Flipkart | |
product_name = "Apple iPhone 12" | |
amazon_url = "https://www.amazon.in/s?k=" + product_name.replace(" ", "+") | |
flipkart_url = "https://www.flipkart.com/search?q=" + product_name.replace(" ", "+") | |
# User-Agent header to pretend as a browser | |
headers = {'User-Agent': 'chrome/58.0.3029.110'} | |
# Get the Amazon page content and parse it with BeautifulSoup | |
amazon_response = requests.get(amazon_url, headers=headers) | |
amazon_soup = BeautifulSoup(amazon_response.content, 'html.parser') | |
# Get the price from Amazon page | |
amazon_price = amazon_soup.find('span', {'class': 'a-price-whole'}).text.strip() | |
# Get the Flipkart page content and parse it with BeautifulSoup | |
flipkart_response = requests.get(flipkart_url, headers=headers) | |
flipkart_soup = BeautifulSoup(flipkart_response.content, 'html.parser') | |
# Get the price from Flipkart page | |
flipkart_price = flipkart_soup.find('div', {'class': '_30jeq3 _1_WHN1'}).text.strip() | |
# To Print the prices | |
print("Amazon price for", product_name, "is", amazon_price) | |
print("Flipkart price for", product_name, "is", flipkart_price) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment