Web Scraping Images Python



  • 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.

  • Mar 10, 2021 Scrapy is a powerful Python web scraping and web crawling framework. Scrapy provides many features to download web pages asynchronously, process them and save them. It handles multithreading, crawling (the process of going from link to link to find every URL in a website), sitemap crawling, and more.
  • Step 1:Install Python 2. Since we will be using Python scripts to extract data from the Facebook page then we need to install Python interpreter to execute them.Installation instructions will vary depending on whether you are using Mac OS X,Linux/UNIX or Windows.I will cover the installation in brief.But it is very easy and there is a lot of detailed instructions online incase you can’t.
Web Scraping Images Python

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.

Web scraping images python programming

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.

In this tutorial, you will learn how you can build a Python scraper that retrieves all images from a web page given its URL and downloads them using requests and BeautifulSoup libraries. To get started, we need quite a few dependencies, let's install them: pip3 install requests bs4 tqdm. Open up a new Python file and import necessary modules.

Getting started

First, we’ll import our libraries.

Next, we’ll generate the CSV file.

Web scraping images python code

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:

Scraping with BeautifulSoup

Web Scraping Images Python Free

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).

Python web scraping sample

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.

Web Scraping Using Python

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

Web Scraping Images Python Online

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