0 %
!
Programmer
SEO-optimizer
English
German
Russian
HTML
CSS
WordPress
Python
C#
  • Bootstrap, Materialize
  • GIT knowledge

Scraping Data: A Step-by-Step Guide and Example

06.01.2024

Web scraping, also known as web data extraction or web harvesting, refers to the automated process of extracting data from websites. It involves using bots or web scrapers to mine data from the web and convert unstructured data into structured data that can be stored and analyzed.

Scraping data from the web has many uses across various industries and disciplines. Common applications include:

  • Price monitoring – Track prices for products on ecommerce sites to find pricing trends.

  • Lead generation – Compile contact details and other information on prospects from business directories.

  • Market research – Gather data on competitors, products, reviews etc. to inform business strategy.

  • News monitoring – Automatically aggregate headlines and articles on relevant topics.

  • Academic research – Collect data from online sources for statistical analysis and modeling.

  • Search engine optimization – Analyze backlink profiles of websites to inform link building.

While scraping can provide valuable data, it does come with some ethical concerns around copyright and fair use of content. Make sure to review a website’s terms of service before scraping and don’t overload servers with an excessive number of requests.

Below we’ll walk through an example of web scraping using Python and Beautiful Soup to extract data from a website into a Pandas dataframe for analysis.

Scraping Data Example Step-by-Step

To follow along with this scraping example, you’ll need:

  • Python installed with the requests, BeautifulSoup, Pandas, and Numpy modules.
  • Basic knowledge of HTML, CSS, and XPath for parsing and extracting data.
  • Familiarity with BeautifulSoup for scraping web pages in Python.
  • Understanding of Pandas for handling scraped data.

1. Import Required Modules

We’ll import the necessary modules for scraping, parsing, analyzing and working with the scraped data:


import requests
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np

2. Define URL and Send Request

We’ll scrape a sample ecommerce website to extract details on fiction books. Define the base URL and use requests to download the webpage content:


url = 'https://books.toscrape.com/catalogue/category/books/mystery_3/index.html'
response = requests.get(url)

Check that a successful response was received with a 200 status code before proceeding.

3. Parse HTML Content

Pass the downloaded HTML content to BeautifulSoup for parsing:


soup = BeautifulSoup(response.text, 'html.parser')

This creates a BeautifulSoup object representing the document structure we can now navigate and search.

4. Extract Data Elements

With the parsed HTML document in a BeautifulSoup object, we can extract the specific data points we want, such as:

  • Book titles
  • Prices
  • Ratings
  • Availability

We locate elements by CSS class or other selectors. For example, book titles are in <a> tags with a title class:


titles = soup.find_all('a', {'class': 'title'})

5. Store in DataFrame

To structure and organize all the extracted data, we’ll store it in a Pandas dataframe:


books_df = pd.DataFrame(
columns=['Title', 'Price', 'Rating', 'Availability'])

for title in titles:
book = title.find('h3').text
price = title.find_next(class_='price_color').text[1:]

rating = title.find_next(class_='star-rating')['class'][1]

availability = title.find_next(class_='instock').text.strip()

books_df = books_df.append({
'Title': book,
'Price': price,
'Rating': rating,
'Availability': availability
}, ignore_index=True)

The final output is a Pandas dataframe containing the scraped data ready for analysis and visualization:

Title Price Rating Availability
0 In a Dark, Dark Wood £7.99 3 In stock
1 The Woman in Cabin 10 £8.99 4 In stock
2 Unravelling Oliver £7.32 3 In stock

This demonstrates a basic example of how to leverage Python libraries like BeautifulSoup to scrape, structure and store data from websites. The scraped data can then be further processed and analyzed to extract insights.

When parsing any website, be sure to check their terms of service and avoid causing excessive load on their servers. Use scraped data responsibly.

Posted in Python, ZennoPosterTags:
Write a comment
© 2024... All Rights Reserved.

You cannot copy content of this page