Skip to content
Snippets Groups Projects
Commit 97f84c9d authored by Andy Kuemmel's avatar Andy Kuemmel
Browse files

Delete lec_31_web3_template.ipynb

parent df4d7c8d
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# Web3: Scraping Web Data
%% Cell type:code id: tags:
``` python
# import statements
import requests
from bs4 import BeautifulSoup # bs4 is the module, BeautifulSoup is the type
```
%% Cell type:markdown id: tags:
### Warmup 1: HTML table and hyperlinks
In order to scrape web pages, you need to know the HTML syntax for tables and hyperlinks.
TODO: Add another row or two to the table below
%% Cell type:markdown id: tags:
<table>
<tr>
<th>University</th>
<th>Department</th>
</tr>
<tr>
<td>UW-Madison</td>
<td><a href = "https://www.cs.wisc.edu/">Computer Sciences</a></td>
</tr>
<tr>
<td>UW-Madison</td>
<td><a href = "https://stat.wisc.edu/">Statistics</a></td>
</tr>
<tr>
<td>UW-Madison</td>
<td><a href = "https://cdis.wisc.edu/">CDIS</a></td>
</tr>
<tr>
<td>UC Berkeley</td>
<td><a href = "https://eecs.berkeley.edu/">Electrical Engineering and Computer Sciences</a></td>
</tr>
</table>
%% Cell type:markdown id: tags:
### Warmup 2: Scraping data from syllabus page
URL: https://www.msyamkumar.com/cs220/s22/syllabus.html
%% Cell type:code id: tags:
``` python
# Get this page using requests.
url = "https://www.msyamkumar.com/cs220/s22/syllabus.html"
# make sure there is no error
# read the entire contents of the page into a single string variable
# split the contents into list of strings using newline separator
```
%% Cell type:markdown id: tags:
#### Warmup 2a: Find all sentences that contain "Meena"
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
#### Warmup 2b: Extract title tag's value
%% Cell type:code id: tags:
``` python
# finally, we are able to extract the title tag's data
# Takeaway: It would be nice if there were a module that could make finding easy!
```
%% Cell type:markdown id: tags:
### Learning Objectives:
- Using the Document Object Model of web pages
- describe the 3 things a DOM element may contain, and give examples of each
- given an html string, identify the correct DOM tree of elements
- Create BeautifulSoup objects from an html string and use prettify to display
- Use the BeautifulSoup methods 'find' and 'find_all' to find particular elements by their tag
- Inspect a BeautufulSoup element to determine the contents of a web page using get_text(), children, and attrs
- Use BeautifulSoup to scrape a live web site.
%% Cell type:markdown id: tags:
### Document Object Model
In order to render a HTML page, most web browsers use a tree structure called Document Object Model (DOM) to represent the HTML page as a hierarchy of elements.
<div>
<img src="attachment:image.png" width="600"/>
</div>
%% Cell type:markdown id: tags:
### Take a look at the HTML in the below cell.
%% Cell type:markdown id: tags:
<b>To Do List</b>
<ul>
<li>Eat Healthy</li>
<li>Sleep <b>More</b></li>
<li>Exercise</li>
</ul>
%% Cell type:markdown id: tags:
### BeautifulSoup constructor
- takes a html, as a string, as argument and parses it
- Syntax: `BeautifulSoup(<html_string>, "html.parser")`
- Second argument specifies what kind of parsing we want done
%% Cell type:code id: tags:
``` python
html_string = "<b>To Do List</b><ul><li>Eat Healthy</li><li>Sleep <b>More</b></li><li>Exercise</li></ul>"
type(bs_obj)
```
%% Cell type:markdown id: tags:
## BeautifulSoup operations
- `prettify()` returns a formatted representation of the raw HTML
### A BeautifulSoup object can be searched for elements using:
- `find("")` returns the first element matching the tag string, None otherwise
- `find_all("")` returns an iterable of all matching elements (HTML 'tags'), empty iterable otherwise
### Beautiful Soup Elements can be inspected by using:
- `get_text()` returns the text associated with this element, if applicable; does not return the child elements associated with that element
- `.children` all children of this element (can be converted into a list)
- `.attrs` the atribute associated with that element / tag.
%% Cell type:markdown id: tags:
`prettify()` returns a formatted representation of the raw HTML
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
`find` returns the first HTML 'tag' matching the string "b"
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
What is the type of find's return value?
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
How do we extract the text of the "b" element and what is its type?
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
`find` returns None if it cannot find that element.
%% Cell type:code id: tags:
``` python
# assert that this html string has a <ul> tag
# assert that this does not have an <a> tag
```
%% Cell type:markdown id: tags:
`find_all` returns an iterable of all matching elements (HTML 'tags') matching the string "b"
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
What is the type of return value of `find_all`?
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
Use a for loop to print the text of each "b" element.
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
Unlike `find`, `find_all` returns an empty iterable, when there are no matching elements.
%% Cell type:code id: tags:
``` python
# only searches for elements, not text
print(bs_obj.find_all("Sleep"))
# if not present returns None
print(bs_obj.find("Sleep"))
```
%% Cell type:markdown id: tags:
You can invoke `find` or `find_all` on other BeautifulSoup object instances.
Find all `li` elements and find `b` element inside the second `li` element.
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
DOM trees are hierarchical. You can use `.children` on any element to gets its children.
%% Cell type:markdown id: tags:
Find all the children of "ul" element.
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
Find text of every child element.
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
Notice that `get_text()` only returns the actual text and not the HTML formatting. For example, part of second child element's text is enclosed within `<b>More</b>`. Despite that `get_text()`
%% Cell type:markdown id: tags:
To understand `attribute`, let's go back to the table from warmup 1.
New syntax, you can use `"""some really long string"""` to split a string across multiple lines.
%% Cell type:code id: tags:
``` python
html_string = """
<table>
<tr>
<th>University</th>
<th>Department</th>
</tr>
<tr>
<td>UW-Madison</td>
<td><a href = "https://www.cs.wisc.edu/">Computer Sciences</a></td>
</tr>
<tr>
<td>UW-Madison</td>
<td><a href = "https://stat.wisc.edu/">Statistics</a></td>
</tr>
<tr>
<td>UW-Madison</td>
<td><a href = "https://cdis.wisc.edu/">CDIS</a></td>
</tr>
<tr>
<td>UC Berkeley</td>
<td><a href = "https://eecs.berkeley.edu/">Electrical Engineering and Computer Sciences</a></td>
</tr>
</table>
"""
```
%% Cell type:markdown id: tags:
Find the table headers.
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
Find the first anchor element, extract its text.
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
You can get the attributes associated with an element using `.attrs` on that element object. Return value will be a `dict` mapping each attribute to its value.
Now, let's get the attributes of the anchor element.
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
What is the return value type of `.attrs`?
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
Extract the hyperlink.
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
Extract hyperlinks for each department and populate department name and link into a `dict`.
%% Cell type:code id: tags:
``` python
department_urls = {} # Key: department name; Value: website URL
```
%% Cell type:markdown id: tags:
#### Self-practice: Extract title of the CS220 syllabus page (from warmup 2)
%% Cell type:code id: tags:
``` python
# Get this page using requests.
url = "https://www.msyamkumar.com/cs220/s22/syllabus.html"
# make sure there is no error
# read the entire contents of the page into a single string variable
# use BeautifulSoup to extract title
```
%% Cell type:markdown id: tags:
## Parsing small_movies html table to extract `small_movies.json`
%% Cell type:markdown id: tags:
### https://www.msyamkumar.com/cs220/f21/syllabus.html
%% Cell type:markdown id: tags:
### Step 1: Read `small_movies.html` content into a variable
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
### Step 2: Initialize BeautifulSoup object instance
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
### Step 3: Find table element
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
### Step 4: Find all th tags, to parse the table header
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
### Step 5: Scrape second row, convert data to appropriate types, and populate data into a row dictionary
- "Year", "Runtime": `int` conversion
- "Revenue": format_revenue(...) conversion
- "Rating": `float` conversion
%% Cell type:code id: tags:
``` python
def format_revenue(revenue):
if type(revenue) == float: # need this in here if we run code multiple times
return revenue
elif revenue[-1] == 'M': # some have an "M" at the end
return float(revenue[:-1]) * 1e6
else: # otherwise, assume millions.
return float(revenue) * 1e6
```
%% Cell type:code id: tags:
``` python
# Why second row? Because first row has the header information.
```
%% Cell type:markdown id: tags:
### Step 6: Scrape all rows, convert data to appropriate types, and populate data into a row dictionary and append row dictionaries into a list
- "Year", "Runtime": `int` conversion
- "Revenue": format_revenue(...) conversion
- "Rating": `float` conversion
You can compare your parsing output to `small_movies.json` file contents, to confirm your result.
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
### Final step: convert steps 1 through 6 into a function and use that function to parse `full_movies.html` file.
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
full_movies_data = parse_html("full_movies.html")
# full_movies_data
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment