A document from MCS 275 Spring 2021, instructor David Dumas. You can also get the notebook file.

Worksheet 15

MCS 275 Spring 2021 - David Dumas

Topics

This worksheet focuses on urllib and Beautiful Soup.

The main references for these topics are:

Instructions

  • Problem 1 is handled differently than the others:
    • Tuesday discussion students: Problem 1 will be presented as an example at the start of discussion
    • Thursday discussion students: Please complete problem 1 before discussion and bring your solution to discussion
  • For the other problems:
    • Work on these problems in discussion.

1. HTML prettifier and warning utility

Use Beautiful Soup to write a script that takes an HTML file and writes a version of it with nicer indentation to an output HTML file. Also, if there is no <title> tag in the input HTML file, the script should print a warning.

The input HTML filename should be expected as the first command line argument, and the filename to which the prettified HTML is written is the second command line argument.

For example, if the in.html contains

<!doctype html><html><head></head><body>
<h1>MCS 275 HTML file</h1></body></html>

Then running

python3 prettify.py in.html out.html

should print a message

Warning: This HTML file has no <title>.

and should write the following to out.html:

<!DOCTYPE html>
<html>
 <head>
 </head>
 <body>
  <h1>
   MCS 275 HTML file
  </h1>
 </body>
</html>

2. Complex analysis homework scraper

Consider this web page for a graduate complex analysis class that was taught at UIC in 2016:

One section of the page lists weekly homework. Each homework assignment has a number, a title, and a list of problems from various sections of the textbook. Write a scraper that downloads this course web site's HTML, parses it with Beautiful Soup, and creates one dictionary for each homework assignment having the following format

{
  "number": 10,
  "title": "Harmonic functions",
  "problems": "Sec 4.6.2(p166): 1,2\nSec 4.6.4(p171): 1,2,3,4"
}

It should then put these dictionaries into a list and save the list to a JSON file called math535spring2016homework.json.

Note: If you finish this problem early, you might find it fun to watch this animation of the UIC logo distortion that appears on the Math 535 course web page, and see if you can figure out what's going on.

3. Capture the tag

Here is a link to an HTML file:

If you open it in a browser, you won't see anything. The document contains nothing but <span> tags, and no text. Some of the <span> tags are nested inside other <span> tags. How deeply are they nested?

Every <span> tag in this file has an id attribute. There is exactly one <span> that has greater depth in the the DOM tree than any other. What is its id attribute?

Write a Python script to load the HTML file with Beautiful Soup and tranverse the DOM to answer these questions.

Course evaluation reminder

If you finish the exercises above, this would be a good time to complete your MCS 275 course evaluation. This anonymous survey helps UIC improve its courses and teaching. Every student enrolled in the course received a link to complete such a survey by email to their uic.edu account, sent by UICTEACHINGEVAL@uic.edu.

Evaluations for 15-week Spring 2021 courses are due by 11:55pm on Sunday, May 2.