In this article, we will learn how to use Python and the BeautifulSoup module to download all the images from a Wikipedia page.
—-
Overview
The goal is to extract the names, breed groups, local names, and image URLs for all dog breeds listed on this Wikipedia page. We will store the image URLs, download the images and save them to a local folder.
Here are the key steps we will cover:
- Import required modules
- Send HTTP request to fetch the Wikipedia page
- Parse the page HTML using BeautifulSoup
- Find the table with dog breed data
- Iterate through the table rows
- Extract data from each column
- Download images and save locally
- Print/process extracted data
Let's go through each of these steps in detail.
Imports
We begin by importing the required modules:
import os
import requests
from bs4 import BeautifulSoup
Make sure you have these modules installed before running the code.
Send HTTP Request
To download the web page containing the dog breed table, we need to send an HTTP GET request:
url = '<https://commons.wikimedia.org/wiki/List_of_dog_breeds>'
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
}
response = requests.get(url, headers=headers)
We provide a user-agent header to mimic a browser request. The
Parse HTML with BeautifulSoup
To extract data from the page, we need to parse the HTML content. BeautifulSoup makes this easy:
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
The
Find Breed Table
The dog breed data we want is contained in a table with class
table = soup.find('table', {'class': 'wikitable sortable'})
This returns a Now we loop through the rows, skipping the header row: The Inside the loop, we extract the data from each column: We find all To download the images: We send another GET request, this time using the image URL. If successful, we construct a file path using the breed name, write the image bytes to that file. Finally, we store the extracted data in lists: These lists can then be processed or printed as needed. And that's it! Here is the full code: This gives you a complete template to extract data from HTML tables into structured lists and download associated images. The same technique can be applied to many different websites. While these examples are great for learning, scraping production-level sites can pose challenges like CAPTCHAs, IP blocks, and bot detection. Rotating proxies and automated CAPTCHA solving can help. Proxies API offers a simple API for rendering pages with built-in proxy rotation, CAPTCHA solving, and evasion of IP blocks. You can fetch rendered pages in any language without configuring browsers or proxies yourself. This allows scraping at scale without headaches of IP blocks. Proxies API has a free tier to get started. Check out the API and sign up for an API key to supercharge your web scraping. With the power of Proxies API combined with Python libraries like Beautiful Soup, you can scrape data at scale without getting blocked.
Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you
curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com" <!doctype html> Enter your email below to claim your free API key: element.
Iterate Through Rows
for row in table.find_all('tr')[1:]:
# extract data from columns
...
Extract Column Data
columns = row.find_all(['td', 'th'])
name = columns[0].find('a').text.strip()
group = columns[1].text.strip()
span_tag = columns[2].find('span')
local_name = span_tag.text.strip() if span_tag else ''
img_tag = columns[3].find('img')
photograph = img_tag['src'] if img_tag else ''
and elements and get the text from them. For the image URL, we check if an Download Images
if photograph:
response = requests.get(photograph)
if response.status_code == 200:
image_filename = os.path.join('dog_images', f'{name}.jpg')
with open(image_filename, 'wb') as img_file:
img_file.write(response.content)
Store Extracted Data
names.append(name)
groups.append(group)
local_names.append(local_name)
photographs.append(photograph)
# Full code
import os
import requests
from bs4 import BeautifulSoup
url = '<https://commons.wikimedia.org/wiki/List_of_dog_breeds>'
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
table = soup.find('table', {'class': 'wikitable sortable'})
names = []
groups = []
local_names = []
photographs = []
os.makedirs('dog_images', exist_ok=True)
for row in table.find_all('tr')[1:]:
columns = row.find_all(['td', 'th'])
name = columns[0].find('a').text.strip()
group = columns[1].text.strip()
span_tag = columns[2].find('span')
local_name = span_tag.text.strip() if span_tag else ''
img_tag = columns[3].find('img')
photograph = img_tag['src'] if img_tag else ''
if photograph:
response = requests.get(photograph)
if response.status_code == 200:
image_filename = os.path.join('dog_images', f'{name}.jpg')
with open(image_filename, 'wb') as img_file:
img_file.write(response.content)
names.append(name)
groups.append(group)
local_names.append(local_name)
photographs.append(photograph)
Browse by language:
The easiest way to do Web Scraping
Try ProxiesAPI for free
<html>
<head>
<title>Example Domain</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
...Don't leave just yet!