क्रिश्चियन शॉ
  • होम
  • Blog
    • प्रोग्रामिंग
      • C#
      • पॉवरशेल
      • Python
      • SQL
    • वर्डप्रेस
      • ट्युटोरियल
    • क्लाउड
    • होम ऑटोमेशन
      • होम असिस्टेंट
        • Node-Red
    • Career
  • सेवाएं
  • शब्दकोष
  • About
No Result
View All Result
क्रिश्चियन शॉ
  • होम
  • Blog
    • प्रोग्रामिंग
      • C#
      • पॉवरशेल
      • Python
      • SQL
    • वर्डप्रेस
      • ट्युटोरियल
    • क्लाउड
    • होम ऑटोमेशन
      • होम असिस्टेंट
        • Node-Red
    • Career
  • सेवाएं
  • शब्दकोष
  • About
No Result
View All Result
क्रिश्चियन शॉ
No Result
View All Result
Home प्रोग्रामिंग Python
scrape prices, web scrape

How to scrape prices from competitors e-commerce websites using Python

by ईसाई
2022-7-जनवरी
in Python
6

Running an e-commerce business in today’s world can be quite a job. Everyone is trying to beat each other with different tactics in marketing campaigns, prices, customer service, etc… To keep up with your competitors it’s important that you know their prices for the same products as yours to stay just below them to attract the customers.

With that said, let’s take a look at how you can scrape prices from your competitor’s e-Commerce website for prices on products that matches your products.

विषय-सूची
  1. Common problems with tracking competitors pricing
  2. The Scenario – Scrape prices from competitor
  3. Scrape the websites for prices using Python and Selenium
    • Install requirements
    • Inspect websites for price elements
    • Write the script for extracting the price from the website
    • Final Web Scrape Script
  4. Summary
    • Don’t violate copyright

Common problems with tracking competitors pricing

Back in the days, an organization had to manually go to the competitors’ store, check their prices and then return to their company and update the product catalog with new prices. This used to require a lot of human resources and was very time-consuming. As more and more companies turn on for the e-commerce part of their business, the prices for products in a digital world become more transparent. This gives customers the option to check for the best price every time they want to shop for a new product.

In this short tutorial, I will teach you how to use selenium with Python to create a script that will extract prices from your competitors’ websites and store the values in a CSV for further processing.

The Scenario – Scrape prices from competitor

Let’s imagine you got an e-commerce website and you sell dog equipment. Lots of other websites out there are also selling dog equipment and some of them even got the same products as you do. To get an advantage over your competitors, you need to check their product prices every day, or hour to see if there are any changes in their pricing and update your shop accordingly.

To do this we need to do some web scraping using Python. To achieve this we will be using a Python package named Selenium with a chrome driver to inspect price elements on the websites.

For this example, I will be scraping the price of four different websites for one single product named Ruffwear Cloud Chaser Jacket. It’s a jacket for dogs made by Ruff Wear. Below is an example of the jacket on trekkinn.com.

scrape prices
Ruffwear Cloud Chaser Jacket at trekinn.com

Scrape the websites for prices using Python and Selenium

The first thing we have to do is find the websites we would like to scrape. I will be scraping 4 websites for this demo:

  • Ruffwear Cloud Chaser™ » waterproof softshell dog jacket (woofshack.com)
  • Cloud Chaser™ Dog Jacket | Ruffwear
  • Ruffwear Cloud Chaser Jacket Grey buy and offers on Trekkinn
  • Ruffwear Cloud Chaser Jacket Grey buy and offers on Bricoinn

Install requirements

Let’s prepare our code. First, you need to make sure that you have selenium installed on your machine.

# Install Selenium uisng PIP
pip install -U selenium

# Download the webdriver for Selenium
https://sites.google.com/chromium.org/driver/downloads

Inspect websites for price elements

Go to one of the websites you would like to scrape the price at and mark the price, right click the marked text and select Inspect. Now you have to locate the price element inside the source code.

web scrape, scrape prices
Locate Price element on a website

Write the script for extracting the price from the website

Get the element by id from the source code. The web driver from Chrome has to be in the same folder as your python script. Let’s test that we can get the price from the above page:

from selenium import webdriver
import time

# Get the website using the Chrome webbdriver
browser = webdriver.Chrome()
browser.get('https://www.woofshack.com/en/cloud-chaser-waterproof-softshell-dog-jacket-ruffwear-rw-5102.html')

# Print out the result
price = browser.find_element_by_id('product-price-665')
print("Price: " + price.text)

# Close the browser
time.sleep(3)
browser.close()

Result:

PS C:\Users\Christian Schou\OneDrive\Desktop\price-scraper> python .\extract-prices.py

DevTools listening on ws://127.0.0.1:62089/devtools/browser/14418422-4293-492b-b3ff-aad1a5f62ff4
Price: €107.95

Now we have to store the data into a data frame that can be inserted into an excel sheet in the end. Make sure you have pandas installed, if not you can use this command: pip install pandas.

from selenium import webdriver
import time

# Get the website using the Chrome webbdriver
browser = webdriver.Chrome()
browser.get('https://www.woofshack.com/en/cloud-chaser-waterproof-softshell-dog-jacket-ruffwear-rw-5102.html')

# Print out the result
price = browser.find_element_by_id('product-price-665')
print("Price: " + price.text)

# Close the browser
time.sleep(3)
browser.close()

#store it into a data frame for saving to Excel at a later time in the script
import numpy as np
import pandas as pd
df = pd.DataFrame([["woofshack.com", price.text]], columns=["Website","Price"])

Now do the same thing for the other website like below.

from selenium import webdriver
import time

# Get the website using the Chrome webbdriver
browser = webdriver.Chrome()
browser.get('https://www.woofshack.com/en/cloud-chaser-waterproof-softshell-dog-jacket-ruffwear-rw-5102.html')

# Print out the result
price = browser.find_element_by_id('product-price-665')
print("Price: " + price.text)

#store it into a data frame for saving to Excel at a later time in the script
import numpy as np
import pandas as pd
df = pd.DataFrame([["woofshack.com", price.text]], columns=["Website","Price"])

# Close the browser
#time.sleep(3)
#browser.close()

#Repeat the step for website no. 2, etc...
browser.get('https://www.bricoinn.com/en/ruffwear-cloud-chaser-jacket/138328147/p')
price = browser.find_element_by_id('datos_producto_precio')
print("Price: " + price.text)


#Put in the product B price into the table
df2 = pd.DataFrame([["bricoinn.com", price.text]], columns=["Website","Price"])
df=df.append(df2, ignore_index=True)

#Repeat the step for website no. 3, etc...
browser.get('https://www.trekkinn.com/outdoor-mountain/ruffwear-cloud-chaser-jacket/138328147/p')
price = browser.find_element_by_id('datos_producto_precio')
print("Price: " + price.text)


#Put in the product C price into the table
df3 = pd.DataFrame([["trekkinn.com", price.text]], columns=["Website","Price"])
df=df.append(df3, ignore_index=True)

print(df)

# Close the browser

browser.close()

Running the small web scrape script gave me this in my console:

PS C:\Users\Christian Schou\OneDrive\Skrivebord\deep-learning> python .\extract-prices.py

DevTools listening on ws://127.0.0.1:55039/devtools/browser/38f8468b-1eaf-4a37-b2f0-9d8692b83bb7

    Website          Price
0   woofshack.com    €107.95
1   bricoinn.com     kr 792.99
2   trekkinn.com     kr 792.99

Now the only thing we have to do in order to save the result to an Excel sheet is append the following line of code at the bottom of the script: df.to_csv(r'PriceList.csv', index = False).

The Excel CSV file looks like the following when data has been imported:

Python Price Scrape Result imported to Excel

Final Web Scrape Script

Below is the final script to scrape prices from your competitors.

from selenium import webdriver
import time

# Get the website using the Chrome webbdriver
browser = webdriver.Chrome()
browser.get('https://www.woofshack.com/en/cloud-chaser-waterproof-softshell-dog-jacket-ruffwear-rw-5102.html')

# Print out the result
price = browser.find_element_by_id('product-price-665')
print("Price: " + price.text)

#store it into a data frame for saving to Excel at a later time in the script
import numpy as np
import pandas as pd
df = pd.DataFrame([["woofshack.com", price.text]], columns=["Product","Price"])

# Close the browser
#time.sleep(3)
#browser.close()

#Repeat the step for website no. 2, etc...
browser.get('https://www.bricoinn.com/en/ruffwear-cloud-chaser-jacket/138328147/p')
price = browser.find_element_by_id('datos_producto_precio')
print("Price: " + price.text)


#Put in the product B price into the table
df2 = pd.DataFrame([["bricoinn.com", price.text]], columns=["Product","Price"])
df=df.append(df2, ignore_index=True)

#Repeat the step for website no. 3, etc...
browser.get('https://www.trekkinn.com/outdoor-mountain/ruffwear-cloud-chaser-jacket/138328147/p')
price = browser.find_element_by_id('datos_producto_precio')
print("Price: " + price.text)


#Put in the product C price into the table
df3 = pd.DataFrame([["trekkinn.com", price.text]], columns=["Product","Price"])
df=df.append(df3, ignore_index=True)

print(df)

# Close the browser

browser.close()

#Save data frame data into Excel CSV file
df.to_csv(r'PriceList.csv', index = False)

Summary

You can use the above script for a single product or tweak it to use it for multiple products on one single website. In this short tutorial about Python web scraping, you learned how a simple python script can help you to track competitor prices to your advantage.

Don’t violate copyright

When scraping a website you should always consider whether the web data you are planning to scrape is copyrighted. This includes often content such as Articles, Videos, Pictures, Stories, Music, and Databases. If you intend to scrape data like that, please get an agreement from the author before scraping the data.

You can extend it to do much more like automated emails, data analysis on prices, etc… If you got any issues, questions, or suggestions, please let me know in the comments. If you are interested in Stocks then don’t forget to check out my tutorial on how to scrape Stock Prices from Yahoo Finance. Happy Coding! 🙂

Tags: ChromeCLIDevelopmentPythonScrapingSeleniumWebWeb Scraping
Previous Post

Top 5 things to know about .NET 6

Next Post

How to use API versioning in ASP.NET Core Web API and integrate it with Swagger using .NET 6

ईसाई

ईसाई

Hello 👋 My name is Christian and I am 26 years old. I'm an educated Software Developer with a primary focus on C#, .NET Core, Python, and PowerShell. Currently, I'm expanding my skills in Software Robots and Cloud Architecture. In some of my spare time, I share my knowledge about tech stuff on my blog.

Related Posts

stock price scraper
Python

How to make a STOCK price scraper command-line application using Python and Web Scraping

by ईसाई
2021-6-दिसम्बर
0

If you like me like to check the stock prices from time to time, but don't want to navigate around...

Read more
delete files and folders

How to delete files and folders in a directory older than X days

2021-9-अक्टूबर
Next Post
api versioning

How to use API versioning in ASP.NET Core Web API and integrate it with Swagger using .NET 6

Comments 6

  1. Lapak Brebes says:
    6 महीना ago

    Greetings from Idaho! I’m bored to tears at work so I decided to browse your blog on my iPhone during lunch break.

    I love the knowledge you present here and can’t wait to take a look when I get home.
    I’m surprised at how quickly your blog loaded on my mobile…

    I’m not even using WIFI, just 3G. Anyway, great blog!

    प्रतिक्रिया
    • Christian Schou says:
      6 महीना ago

      Hi Lapak

      Thank you for the kind words! I am still working on improving the page performance, but glad to hear that you already think it is fast! 🙂

      प्रतिक्रिया
  2. William Banning says:
    5 महीना ago

    Please let me know if you’re looking for a article author for your blog. You have some really good articles and I think I would be a good asset.

    If you ever want to take some of the load off, I’d absolutely love to write some content for your blog. Please shoot me an e-mail if interested. Many thanks!

    प्रतिक्रिया
  3. Joseph says:
    5 महीना ago

    Awesome blog article. Thanks Again. Want more.

    प्रतिक्रिया
  4. Mustapha says:
    3 महीना ago

    Can you take a similar approach to scrape the SALE section of a website and get deals with 50% or more discounts, or monitor price of items which become discounted for 50% or more.

    प्रतिक्रिया
    • Christian Schou says:
      3 महीना ago

      Hi Mustapha

      Yes, that is definitely possible. You could make a script to extract all prices on a specific website or multiple websites. Then insert the prices into a database and for each time you do the scraping compare the prices to the one you got in the database to check if the price has dropped 50% or more. You would of course need to update the prices in your database each time you scrape them to keep track of the price changes.

      प्रतिक्रिया

प्रातिक्रिया दे जवाब रद्द करें

आपका ईमेल पता प्रकाशित नहीं किया जाएगा. आवश्यक फ़ील्ड चिह्नित हैं *

क्रिश्चियन शॉ

क्रिश्चियन शॉ

Software Developer

Hello - my name is Christian and I am 26 years old. I'm an educated Software Developer with a primary focus on C#, .NET Core, Python, and PowerShell. Currently, I'm expanding my skills in Software Robots and Cloud Architecture. In some of my spare time, I share my knowledge about tech stuff on my blog.

Recent articles

personal website
Career

Top 6 things to add on your personal website to get hired for a tech job

by ईसाई
2022-7-अगस्त
0

Back in the days before the internet was a thing like it is today, we used to have business cards...

Read more
watchdog

The #1 guide to show real-time .NET 6 logs for Web Apps and APIs in a modern way using WatchDog for Free

2022-13-अगस्त
get hired for a tech job

5 tips to help you get hired for a tech job

2022-31-जुलाई
restful web api

How to build a RESTful Web API using ASP.NET Core and Entity Framework Core (.NET 6)

2022-25-जुलाई
dynamically register entities

How to Dynamically Register Entities in DbContext by Extending ModelBuilder?

2022-23-जुलाई

क्रिश्चियन शॉ

Software Developer

Hello - my name is Christian and I am 26 years old. I'm an educated Software Developer with a primary focus on C#, .NET Core, Python, and PowerShell. Currently, I'm expanding my skills in Software Robots and Cloud Architecture. In some of my spare time, I share my knowledge about tech stuff on my blog.

Recent articles

personal website

Top 6 things to add on your personal website to get hired for a tech job

2022-7-अगस्त
watchdog

The #1 guide to show real-time .NET 6 logs for Web Apps and APIs in a modern way using WatchDog for Free

2022-13-अगस्त
get hired for a tech job

5 tips to help you get hired for a tech job

2022-31-जुलाई
  • hi_INहिन्दी
    • da_DKDansk
    • en_USEnglish
    • de_DEDeutsch
    • pt_BRPortuguês do Brasil
  • Contact
  • गोपनीयता नीति
  • सेवा की शर्तें

© 2022 क्रिश्चियन शॉ - All rights reserved.

No Result
View All Result
  • होम
  • Blog
    • प्रोग्रामिंग
      • C#
      • पॉवरशेल
      • Python
      • SQL
    • वर्डप्रेस
      • ट्युटोरियल
    • क्लाउड
    • होम ऑटोमेशन
      • होम असिस्टेंट
    • Career
  • सेवाएं
  • शब्दकोष
  • About

© 2022 क्रिश्चियन शॉ - All rights reserved.

मैं आपकी वरीयताओं को याद करके और बार-बार आने वाली यात्राओं को याद करके आपको सबसे अधिक प्रासंगिक अनुभव देने के लिए अपनी वेबसाइट पर कुकीज़ का उपयोग करता हूं। “स्वीकार करें” पर क्लिक करके, आप सभी कुकीज़ के उपयोग के लिए सहमति देते हैं।
मेरी निजी जानकारी न बेचें.
कुकी सेटिंगACCEPT
गोपनीयता और कुकीज़ नीति

गोपनीयता अवलोकन

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
CookieDurationDescription
__gads1 year 24 daysThe __gads cookie, set by Google, is stored under DoubleClick domain and tracks the number of times users see an advert, measures the success of the campaign and calculates its revenue. This cookie can only be read from the domain they are set on and will not track any data while browsing through other sites.
_ga2 yearsThe _ga cookie, installed by Google Analytics, calculates visitor, session and campaign data and also keeps track of site usage for the site's analytics report. The cookie stores information anonymously and assigns a randomly generated number to recognize unique visitors.
_ga_0J2F6JVWSD2 yearsThis cookie is installed by Google Analytics.
_gat_gtag_UA_84232734_11 minuteSet by Google to distinguish users.
_gid1 dayInstalled by Google Analytics, _gid cookie stores information on how visitors use a website, while also creating an analytics report of the website's performance. Some of the data that are collected include the number of visitors, their source, and the pages they visit anonymously.
YouTube2 yearsYouTube sets this cookie via embedded youtube-videos and registers anonymous statistical data. I embed YouTube videos in my articles/tutorials - you won't get the full experience of the articles if this is deactivated.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
CookieDurationDescription
IDE1 year 24 daysGoogle DoubleClick IDE cookies are used to store information about how the user uses the website to present them with relevant ads and according to the user profile.
test_cookie15 minutesThe test_cookie is set by doubleclick.net and is used to determine if the user's browser supports cookies.
VISITOR_INFO1_LIVE5 months 27 daysA cookie set by YouTube to measure bandwidth that determines whether the user gets the new or old player interface.
YSCsessionYSC cookie is set by Youtube and is used to track the views of embedded videos on Youtube pages.
yt-remote-connected-devicesneverYouTube sets this cookie to store the video preferences of the user using embedded YouTube video.
yt-remote-device-idneverYouTube sets this cookie to store the video preferences of the user using embedded YouTube video.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT
Powered by CookieYes Logo