Skip to content

Instantly share code, notes, and snippets.

@phillipsm
Last active November 25, 2022 14:02
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save phillipsm/0ed98b2585f0ada5a769 to your computer and use it in GitHub Desktop.
Save phillipsm/0ed98b2585f0ada5a769 to your computer and use it in GitHub Desktop.
Example of parsing a table using BeautifulSoup and requests in Python
import requests
from bs4 import BeautifulSoup
# We've now imported the two packages that will do the heavy lifting
# for us, reqeusts and BeautifulSoup
# Let's put the URL of the page we want to scrape in a variable
# so that our code down below can be a little cleaner
url_to_scrape = 'http://apps2.polkcountyiowa.gov/inmatesontheweb/'
# Tell requests to retreive the contents our page (it'll be grabbing
# what you see when you use the View Source feature in your browser)
r = requests.get(url_to_scrape)
# We now have the source of the page, let's ask BeaultifulSoup
# to parse it for us.
soup = BeautifulSoup(r.text)
# Down below we'll add our inmates to this list
inmates_list = []
# BeautifulSoup provides nice ways to access the data in the parsed
# page. Here, we'll use the select method and pass it a CSS style
# selector to grab all the rows in the table (the rows contain the
# inmate names and ages).
for table_row in soup.select("table.inmatesList tr"):
# Each tr (table row) has three td HTML elements (most people
# call these table cels) in it (first name, last name, and age)
cells = table_row.findAll('td')
# Our table has one exception -- a row without any cells.
# Let's handle that special case here by making sure we
# have more than zero cells before processing the cells
if len(cells) > 0:
# Our first name seems to appear in the second td element
# that ends up being the cell called 1, since we start
# counting at 0
first_name = cells[1].text.strip()
# Our last name is in the first (0 if we start counting
# at 0 like we do in Python td element we encounter
last_name = cells[0].text.strip()
# Age seems to be in the last td in our row
age = cells[2].text.strip()
# Let's add our inmate to our list in case
# We do this by adding the values we want to a dictionary, and
# appending that dictionary to the list we created above
inmate = {'first_name': first_name, 'last_name': last_name, 'age': age}
inmates_list.append(inmate)
# Let's print our table out.
print "Added {0} {1}, {2}, to the list".format(first_name, last_name, age)
# What if we want to do more than just print out all the names and
# ages? Maybe we want to filter things a bit. Say, only we want to
# only print out the inmates with an age between 20 and 30.
# Let's keep track of the number of inmates in the 20s.
inmates_in_20s_count = 0
# Loop through the list of inmates we built
for inmate in inmates_list:
# The age we originally received from BeautifulSoup is a
# string. We need it to be a number so that we can compare
# it easily. Let's make it an integer.
age = int(inmate['age'])
if age > 19 and age < 31:
# Let's print our table out.
print "{0} {1}, {2} is in the 20 to 30 age range".format(inmate['first_name'], inmate['last_name'], age)
# Add one to our inmates in their 20s count
inmates_in_20s_count = inmates_in_20s_count + 1
# How many inmates did we find in the page? Use the len funciton to find out.
print "Found {0} in the page".format(len(inmates_list))
print "Found {0} between age 20 and 30 in the page".format(inmates_in_20s_count)
@uhayatpk
Copy link

uhayatpk commented Apr 2, 2018

Hello phillipsm,

Where from you get the inmatesList.
because I have the same problem but don't find any from table.

@MLSMIT
Copy link

MLSMIT commented Apr 10, 2018

Hi phillipsm
I have the same doubt: how do you find that the name of the table is inmateList. I have not been able to find it in inspecting the HTML

@Abdulkereem
Copy link

Put more explanation please

@fornitsumfornis
Copy link

I too don't quite understand where you got inmatesList from. I tried to run the code but I got multiple errors. Am I missing something?

@4sushi
Copy link

4sushi commented Sep 3, 2018

Other implementation with support colspan and rowspan (merged cells) : https://github.com/4sushi/htmlParse

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment