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

MCS 275 Spring 2024 Homework 14

  • Course Instructor: Emily Dumas

Deadline

This homework assignment must be submitted in Gradescope by Noon central time on Tuesday April 23, 2024.

This is the LAST homework assignment in MCS 275. (All that's left after this is to submit Project 4.)

Collaboration

Collaboration is prohibited, and while working on this you should only consult the resources (books, online, etc.) listed below.

Content

This assignment corresponds to Worksheet 14 and is about urllib and the HTML parsing module beautiful soup.

Resources you may consult

The materials you may refer to for this homework are:

Point distribution

This homework assignment has two problems. The grading breakdown is:

Points Item
4 Autograder syntax checks (problem 1)
5 Problem 2
5 Problem 3
14 Total

What to do if you're stuck

Ask your instructor or TA a question by email, in office hours, or on discord.

2. International Space Station hemisphere

The International Space Station (ISS) is a crewed scientific research station in orbit around the earth. At any given time, it is about 220 miles above some location on the earth's surface.

Write a Python script that prints a single sentence to the terminal, either

The International Space Station (ISS) is over the NORTHERN hemisphere right now.

or

The International Space Station (ISS) is over the SOUTHERN hemisphere right now.

according to the current location of the ISS. Retrieve the location using the JSON object available from https://api.wheretheiss.at/v1/satellites/25544 which is part of the API documented here.

(The grading of the assignment will not take into account the behavior of your program when the ISS is directly over the equator.)

3. HTML to image

First, a quick note about HTML: In an HTML document, you can make a section of text (or even a single character) appear in a specific color as follows:

<span style="color: #8800aa;">Deep purple</span>

which gives a result that looks like this:

Deep purple

When this technique is used, it means the color of the text is found somewhere in the value of the style attribute of a span tag that contains the text.

Now, here's a web page that contains a picture of a cow:

But if you look at the source of the page, you'll see the page doesn't really contain an image at all. Instead, it has many paragraphs full of unicode block characters (which look like █ and are created with &block;). Each block character is given a different color, so the resulting grid of blocks looks a bit like an image.

Write a Python script that downloads this HTML document linked above (the one the cow "image") and then uses bs4 (beautiful soup) to parse the HTML and traverses the DOM to extract the color data. It should then write a PNG image called moo.png in which each pixel corresponds to one of the block characters in the web page (and has the same color). Use pillow (PIL.Image) to create the image file.

When you're done, moo.png should look like a very small image of a cow. Also, it will be a bit vertically squashed, because pixels are usually square while unicode characters are usually about twice as tall as they are wide. If you've done everything correctly, the final image should have a width of 130 pixels and a height of 43 pixels.

For your convenience, here is a function that takes a string like #8800aa and returns a tuple of integers like (136, 0, 170).

In [3]:
def hex_color_to_triple(s):
    "Convert a 6-hex-digit color specification to 3 integers"
    s = s.strip("# ")
    if len(s)!=6:
        raise ValueError("Malformed color specification")
    r = int(s[:2],16)
    g = int(s[2:4],16)
    b = int(s[4:],16)
    return (r,g,b)

Revision history

  • 2024-04-18 Initial publication