Web Scraping Examples



Many companies do not allow scraping on their websites, so this is a good way to learn. Just make sure to check before you scrape. Introduction to Web Scraping classroom Preview of codedamn classroom. If you want to code along, you can use this free codedamn classroom that consists of multiple labs to help you learn web scraping. For example, I sometimes have to copy and paste a table from a web page into Google Sheets or fetch the article title or product name from a web page into Google Sheets. Since I spend a lot of my time in Google Sheets anyway, I thought of figuring out if I could scrape the data from the websites and extract the data into the cells in Google Sheets. Scraping the Monster Job Site. In this tutorial, you’ll build a web scraper that fetches Software Developer job listings from the Monster job aggregator site. Your web scraper will parse the HTML to pick out the relevant pieces of information and filter that content for specific words. It’s possible to do a quick search, find a website listing them and scrape it for the content. Simply open the web page with C# to get the content, find keywords and scrape the data. Web scraping with this HTML Agility Pack example. HTML Agility Pack is a free and open source tool that is really useful to get the nodes we want from a web page. For example, Selenium is a portable framework that allows you to automate the functionalities of web browsers using a wide range of programming languages. Whereas it’s primarily used for testing web applications automatically, it can also be used for extracting online data. In this post, we'll take a look at.

So you just discovered web scraping and you’re excited to get started on your first web scraping project.

But sometimes, it’s hard to get your creative juices going and come up with an idea for your first project.

Today, we will propose a couple of ideas that can get you started with web scraping.

What is Web Scraping?

Before we get started, you might be wondering what web scraping is in the first place. In short, web scraping refers to the extraction of data from a website on to a more useful format.

In most cases, web scraping is done with an automated software tool rather than manually. If you’d like to learn more about web scraping, check our in-depth guide on web scraping and what it used for.

Web Scraping Ideas

We have put together 5 different ideas for you to start your first web scraping project.

We have built some of these examples to also allow you to realize the power of web scraping with further analysis.

Taking Price Comparison to the Next Level

One project a lot of people like to start with involves scraping ecommerce sites for product data and price comparison. While this project is a good place to get started, we suggest you take it to the next level and analyze the data from your scrape to find the best purchase in a certain category.

For example, you could scrape data about all tablets available on Amazon and analyze the dataset to figure out what is the best bang for your buck when comparing both pricing and review score data. You can make this analysis more detailed by filtering out products with a low amount of reviews.

You’d be looking to answer the question: What is the best rated tablet you can purchase for the lowest amount?

Ready to get started? Here’s our guide on how to scrape Amazon product data.

Build a Simple Investment App (No Coding)

This might project might sound a bit intimidating. However, building a simple investment app is easier than you’d think.

The goal of this app would be to setup your web scraper to scrape a few specific stocks from Yahoo Finance every day. This scrape will then be fed into a Google Spreadsheet and once any stock drops under a specific price, a “buy” notification will be sent to your email.

You can start this project by checking out the following quick guides:

Scrape a Subreddit to Find Popular Topics and Words

If you’re like me, you might have a few subreddits that you love to browse.

Do you sometimes wonder if there are specific words or topics that get more upvotes than others within that community? Or which topics get more comments and create more discussion?

You could scrape this subreddit and create graphs such as word-clouds to present the insights you’ve found.

You could then take these graphs and insights from your project and share them with that specific subreddit to spark further conversations (and get some sweet reddit karma!).

Interested in this project? Check out our guide on how to scrape reddit data.

Scrape a Leads Database for Someone Else (or sell it!)

You might know someone in your family or circle of friends who runs their own business. Why not help them by building a database of leads for their business?

First, you’d have to ask them about the details of their business and what kind of leads they would find valuable. After this, you can setup your web scraper to scrape leads data from the internet to build your database.

If you do not know anyone in your circle that might need a leads database, you could also try to sell it!

Want to complete this project? Here’s our guide on how to use a web scraping for lead generation.

Take on a Real Web Scraping Job

Why not get started with a real-world example of a web scraping job?

Numerous one-off web scraping jobs get posted on job boards every day. These are great to get started with, since they are examples of what web scraping is being used for in the real world.

A great place to start is UpWork, where you can search for “web scraping” jobs and apply to take them up or just complete them regardless for learning purposes.

Here’s the search results page for “web scraping” in UpWork.

Scraping

What Web Scraper Should You Use?

At this point, you might already know what your first web scraping project will be.

However, you might still be wondering what web scraper you should be using to carry out your project. The truth is that the best web scraper for your project might be different depending on the needs of your project.

However, every single project on this list can be completed using ParseHub, a powerful and free web scraper.

What web scraping project will you tackle first?

Table of Contents

Introduction to web scraping

Web scraping is one of the tools at a developer’s disposal when looking to gather data from the internet. While consuming data via an API has become commonplace, most of the websites online don’t have an API for delivering data to consumers. In order to access the data they’re looking for, web scrapers and crawlers read a website’s pages and feeds, analyzing the site’s structure and markup language for clues. Generally speaking, information collected from scraping is fed into other programs for validation, cleaning, and input into a datastore or its fed onto other processes such as natural language processing (NLP) toolchains or machine learning (ML) models. There are a few Python packages we could use to illustrate with, but we’ll focus on Scrapy for these examples. Scrapy makes it very easy for us to quickly prototype and develop web scrapers with Python.

Scrapy vs. Selenium and Beautiful Soup

If you’re interested in getting into Python’s other packages for web scraping, we’ve laid it out here:

Scrapy concepts

Django Web Scraping Examples

Before we start looking at specific examples and use cases, let’s brush up a bit on Scrapy and how it works.

Spiders: Scrapy uses Spiders to define how a site (or a bunch of sites) should be scraped for information. Scrapy lets us determine how we want the spider to crawl, what information we want to extract, and how we can extract it. Specifically, Spiders are Python classes where we’ll put all of our custom logic and behavior.

Selectors:Selectors are Scrapy’s mechanisms for finding data within the website’s pages. They’re called selectors because they provide an interface for “selecting” certain parts of the HTML page, and these selectors can be in either CSS or XPath expressions.

Items:Items are the data that is extracted from selectors in a common data model. Since our goal is a structured result from unstructured inputs, Scrapy provides an Item class which we can use to define how our scraped data should be structured and what fields it should have.

Reddit-less front page

Suppose we love the images posted to Reddit, but don’t want any of the comments or self posts. We can use Scrapy to make a Reddit Spider that will fetch all the photos from the front page and put them on our own HTML page which we can then browse instead of Reddit.

To start, we’ll create a RedditSpider which we can use traverse the front page and handle custom behavior.

Above, we’ve defined a RedditSpider, inheriting Scrapy’s Spider. We’ve named it reddit and have populated the class’ start_urls attribute with a URL to Reddit from which we’ll extract the images.

At this point, we’ll need to begin defining our parsing logic. We need to figure out an expression that the RedditSpider can use to determine whether it’s found an image. If we look at Reddit’s robots.txt file, we can see that our spider can’t crawl any comment pages without being in violation of the robots.txt file, so we’ll need to grab our image URLs without following through to the comment pages.

By looking at Reddit, we can see that external links are included on the homepage directly next to the post’s title. We’ll update RedditSpider to include a parser to grab this URL. Reddit includes the external URL as a link on the page, so we should be able to just loop through the links on the page and find URLs that are for images.

In a parse method on our RedditSpider class, I’ve started to define how we’ll be parsing our response for results. To start, we grab all of the href attributes from the page’s links using a basic XPath selector. Now that we’re enumerating the page’s links, we can start to analyze the links for images.

To actually access the text information from the link’s href attribute, we use Scrapy’s .get() function which will return the link destination as a string. Next, we check to see if the URL contains an image file extension. We use Python’s any() built-in function for this. This isn’t all-encompassing for all image file extensions, but it’s a start. From here we can push our images into a local HTML file for viewing.

To start, we begin collecting the HTML file contents as a string which will be written to a file called frontpage.html at the end of the process. You’ll notice that instead of pulling the image location from the ‘//a/@href/‘, we’ve updated our links selector to use the image’s src attribute: ‘//img/@src’. This will give us more consistent results, and select only images.

As our RedditSpider’s parser finds images it builds a link with a preview image and dumps the string to our html variable. Once we’ve collected all of the images and generated the HTML, we open the local HTML file (or create it) and overwrite it with our new HTML content before closing the file again with page.close(). If we run scrapy runspider reddit.py, we can see that this file is built properly and contains images from Reddit’s front page.

But, it looks like it contains all of the images from Reddit’s front page – not just user-posted content. Let’s update our parse command a bit to blacklist certain domains from our results.

If we look at frontpage.html, we can see that most of Reddit’s assets come from redditstatic.com and redditmedia.com. We’ll just filter those results out and retain everything else. With these updates, our RedditSpider class now looks like the below:

We’re simply adding our domain whitelist to an exclusionary any()expression. These statements could be tweaked to read from a separate configuration file, local database, or cache – if need be.

Python Web Scraping Examples

Kite is a plugin for PyCharm, Atom, Vim, VSCode, Sublime Text, and IntelliJ that uses machine learning to provide you with code completions in real time sorted by relevance. Start coding faster today.

Extracting Amazon price data

If you’re running an ecommerce website, intelligence is key. With Scrapy we can easily automate the process of collecting information about our competitors, our market, or our listings.

For this task, we’ll extract pricing data from search listings on Amazon and use the results to provide some basic insights. If we visit Amazon’s search results page and inspect it, we notice that Amazon stores the price in a series of divs, most notably using a class called .a-offscreen. We can formulate a CSS selector that extracts the price off the page:

With this CSS selector in mind, let’s build our AmazonSpider.

A few things to note about our AmazonSpider class: convert_money(): This helper simply converts strings formatted like ‘$45.67’ and casts them to a Python Decimal type which can be used for computations and avoids issues with locale by not including a ‘$’ anywhere in the regular expression. getall(): The .getall() function is a Scrapy function that works similar to the .get() function we used before, but this returns all the extracted values as a list which we can work with. Running the command scrapy runspider amazon.py in the project folder will dump output resembling the following:

It’s easy to imagine building a dashboard that allows you to store scraped values in a datastore and visualize data as you see fit.

Considerations at scale

As you build more web crawlers and you continue to follow more advanced scraping workflows you’ll likely notice a few things:

  1. Sites change, now more than ever.
  2. Getting consistent results across thousands of pages is tricky.
  3. Performance considerations can be crucial.

Sites change, now more than ever

On occasion, AliExpress for example, will return a login page rather than search listings. Sometimes Amazon will decide to raise a Captcha, or Twitter will return an error. While these errors can sometimes simply be flickers, others will require a complete re-architecture of your web scrapers. Nowadays, modern front-end frameworks are oftentimes pre-compiled for the browser which can mangle class names and ID strings, sometimes a designer or developer will change an HTML class name during a redesign. It’s important that our Scrapy crawlers are resilient, but keep in mind that changes will occur over time.

Web Scraping Example Python

Getting consistent results across thousands of pages is tricky

Slight variations of user-inputted text can really add up. Think of all of the different spellings and capitalizations you may encounter in just usernames. Pre-processing text, normalizing text, and standardizing text before performing an action or storing the value is best practice before most NLP or ML software processes for best results.

Performance considerations can be crucial

You’ll want to make sure you’re operating at least moderately efficiently before attempting to process 10,000 websites from your laptop one night. As your dataset grows it becomes more and more costly to manipulate it in terms of memory or processing power. In a similar regard, you may want to extract the text from one news article at a time, rather than downloading all 10,000 articles at once. As we’ve seen in this tutorial, performing advanced scraping operations is actually quite easy using Scrapy’s framework. Some advanced next steps might include loading selectors from a database and scraping using very generic Spider classes, or by using proxies or modified user-agents to see if the HTML changes based on location or device type. Scraping in the real world becomes complicated because of all the edge cases, Scrapy provides an easy way to build this logic in Python.

This post is a part of Kite’s new series on Python. You can check out the code from this and other posts on our GitHub repository.

Company

Product

Js Web Scraping Examples

Resources

Stay in touch

Get Kite updates & coding tips

Web Scraping Tutorial

Made with in San Francisco