Web Scraping Python Pandas



  • What this does: Scrapes pages to get alt tags and page titles, and saves as CSV
  • Requires: Python Anaconda distribution, basic knowledge of Pandas and HTML structure
  • Concepts covered: Basic scraper with BeautifulSoup, Scrape multiple pages, Loops, Export to CSV
  • Download the entire Python file

Python has a lot of great uses for marketers, and one of the coolest and most practical tools is a web scraper.

There are many situations where you may need to collect data quickly from a website and save into a usable format. One example is getting image alt or title attributes, which have value for SEO purposes.

Web Scraping with Python: Collecting More Data from the Modern Web — Book on Amazon. Jose Portilla's Data Science and ML Bootcamp — Course on Udemy. Easiest way to get started with Data Science. Covers Pandas, Matplotlib, Seaborn, Scikit-learn, and a lot of other useful topics. Web Scraping with Pandas and Beautifulsoup. APIs are not always available. Sometimes you have to scrape data from a webpage yourself. Luckily the modules Pandas and Beautifulsoup can help! Related Course: Complete Python Programming Course & Exercises. Pandas has a neat concept known as a DataFrame.

In this post, we’ll create a simple web scraper in Python that will collect the alt attributes of images and the title of the page on which they appear.

The scraper uses a library called BeautifulSoup. For a full tutorial on using BeautifulSoup, I’d recommend this tutorial, which provides a really great explanation of how it works.

Web Scraping Python Pandas Tutorial

Getting started

Web scraping basically means that, instead of using a browser, we can use Python to send request to a website server, receive the HTML code, then extract the data we want.

Web Scraping Python Pandas

First, we’ll import our libraries.

Next, we’ll generate the CSV file.

Next, we’ll define the URLs we want to scrape in a list.

Then, we’ll create a blank dataframe.

Conceptualizing data scraping

Our end goal for the data is to have two columns. The first column will have the page name and the second column will have the alt attribute. So, it should look a little something like this:

pagenamealt
Blog HomeComputer screen
Blog HomePie chart
PortfolioMountains
PortfolioLake

So, we can conceptualize the scraping process like this:

Python Web Scraping Tutorial

Scraping with BeautifulSoup

Because we’re going to be scraping multiple URLs, we’ll need to create a loop to repeat the steps for each page. Be sure to pay attention to the indents in the code (or download the .py file).

For the page title, we’ll want to scrape the H1 tag. We’ll use the find() function to find the H1 tag. We’ll print that information and also store it as a variable for a later step.

Next, we’ll scrape the images and collect the alt attributes. Because some images like the logo are repeated on every page, I don’t want to scrape these. Instead, I’ll use .find_all() and only return images with the class “content-header”. Once it finds the images, we’ll print the alt attributes.

Because there may be multiple images on the page, we’ll have to create another loop within the larger loop.

Here comes the cool part. We’ll create a variable defined as the alt attribute. Using this and the variable for the H1 tag we created earlier, we’ll couple these and append them to the dataframe. This step will be repeated each time the loop runs, so for every image on the page with the content header class.

Finally, we’ll save our dataframe to a CSV file.

in Analytics / Marketing 0 comments

APIs are not always available. Sometimes you have to scrape data from a webpage yourself. Luckily the modules Pandas and Beautifulsoup can help!

Related Course:Complete Python Programming Course & Exercises

Web scraping

Pandas has a neat concept known as a DataFrame. A DataFrame can hold data and be easily manipulated. We can combine Pandas with Beautifulsoup to quickly get data from a webpage.

If you find a table on the web like this:

We can convert it to JSON with:

And in a browser get the beautiful json output:

Converting to lists

Pandas

Rows can be converted to Python lists.
We can convert it to a dataframe using just a few lines:

Pretty print pandas dataframe

Web Scraping Python Pandas

You can convert it to an ascii table with the module tabulate.
This code will instantly convert the table on the web to an ascii table:
This will show in the terminal as: