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

MCS 275 Spring 2022 Worksheet 15

  • Course instructor: Emily Dumas

Topics

This worksheet focuses on urllib and Beautiful Soup.

Resources

These things might be helpful while working on the problems. Remember that for worksheets, we don't strictly limit what resources you can consult, so these are only suggestions.

1. HTML prettifier and warning utility

Use Beautiful Soup to write a script that takes an HTML file and writes equivalent HTML that is more nicely indented to an output file. (Recall that Beautiful Soup has a method to generate nicely indented HTML for any tag or BeautifulSoup object.)

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.

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