2026-02-11
Getting data off the web: “scraping”.
Looking at the data: more working with words.
robots.txt.The legality and ethics of “scraping the web” are beyond the scope of this course. Considerations:
<root url>/robots.txt. (example: imdb)![image from https://en.wikipedia.org/wiki/Document_Object_Model showing a tree-like arrangement]](resources/DOM-model.png)
Live view: resources/example.html
Draw the document tree!
Use the developer tools!
For instance: right-click on anything; select “inspect”.
Go to uoregon.edu, and
display: none; to the element’s CSS)html) to something. (inspect)We’ll use Beautiful Soup.
For example all <p> tags:
Other arguments narrow things down:
Or, using CSS selectors:
Generally we want the text nested within certain tags. For instance:
Real-world websites are a lot more complex. Let’s have a look at IMSDb.
Goals:
Next let’s look at the script to Interstellar, available in two files: a CSV file of per-line information and a text file with one “line” per line.
| scene_num | what | who | directions | |
|---|---|---|---|---|
| 0 | 0 | direction | NaN | NaN |
| 1 | 0 | direction | NaN | NaN |
| 2 | 0 | direction | NaN | NaN |
| 3 | 0 | direction | NaN | NaN |
| 4 | 0 | direction | NaN | NaN |
| ... | ... | ... | ... | ... |
| 2457 | 491 | scene | NaN | NaN |
| 2458 | 492 | direction | NaN | NaN |
| 2459 | 492 | direction | NaN | NaN |
| 2460 | 492 | direction | NaN | NaN |
| 2461 | 492 | direction | NaN | NaN |
2462 rows × 4 columns
who
COOPER 305
BRAND 174
DOYLE 65
CASE 62
ROTH 60
MURPH 32
TARS 32
TOM 26
DONALD 20
ANSEN 14
ADMINISTRATOR 12
DOCTOR 11
LIU 10
PRINCIPAL 9
NSA AGENT 8
ASSISTANT 6
BRAND'S FATHER 5
BALLPLAYER 4
RIGGS 3
ROBOT 3
FARMER 3
EMILY COOPER 3
OLD ENGINEER 2
GOVERNMENT MAN 1
OLD MAN 1
ENGINEER ROBOT 1
CHINESE OFFICER 1
MURPH'S WIFE 1
WIFE 1
Name: count, dtype: int64
SPACE.
But not the dark lonely corner of it we're used to. This is a glittering inferno -- the center of a distant galaxy.
Suddenly, something TEARS past at incredible speed: a NEUTRON STAR. It SMASHES headlong through everything it encounters... planets, stars. Can anything stop this juggernaut?
It’s… interesting that she says “maybe” a lot more.
Counter({'NOUN': 6822,
'PUNCT': 6141,
'VERB': 5239,
'ADP': 3977,
'PRON': 3943,
'DET': 3936,
'PROPN': 2327,
'AUX': 1958,
'ADJ': 1933,
'ADV': 1714,
'PART': 1034,
'CCONJ': 651,
'SCONJ': 518,
'NUM': 260,
'INTJ': 59,
'X': 2})
| count | PROPN | PUNCT | CCONJ | PART | DET | ADJ | NOUN | ADP | PRON | AUX | VERB | ADV | INTJ | SCONJ | NUM | X | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| who | |||||||||||||||||
| COOPER | 305 | 52 | 571 | 39 | 175 | 203 | 142 | 366 | 218 | 598 | 337 | 496 | 176 | 24 | 82 | 41 | 0 |
| BRAND | 174 | 30 | 441 | 37 | 129 | 196 | 132 | 361 | 159 | 494 | 301 | 421 | 172 | 11 | 67 | 20 | 1 |
| DOYLE | 65 | 8 | 131 | 11 | 41 | 64 | 33 | 107 | 70 | 133 | 87 | 118 | 48 | 2 | 12 | 10 | 0 |
| CASE | 62 | 22 | 153 | 14 | 45 | 90 | 79 | 163 | 68 | 155 | 98 | 147 | 46 | 3 | 20 | 8 | 0 |
| ROTH | 60 | 9 | 165 | 19 | 64 | 87 | 52 | 134 | 79 | 183 | 103 | 182 | 57 | 5 | 22 | 14 | 0 |
Challenge: Does Interstellar pass the Bechdel test?