Lab: Web Data: Extract, Analyze, Visualize
Due: Wednesday, October 14 at 11:59pm · Worth: 16 points
Web scraping lets us collect data from a page without copying each item by hand. You’ll extract information from HTML, then analyze a saved archive of Donald Trump’s tweets and plot how often particular phrases appear.
Start
Download and extract the starter ZIP. Open the inner lab-web-data folder in VS Code, then initialize and publish your repository as lab-web-data. Activate a Python environment, then run:
python -m pip install -r requirements.txt
python download_tweets.pyThe downloader saves ten master_*.json files from the 2009–2018 archive. Keep them in the repository’s top-level folder so your analysis script can find them.
Extract and analyze
Complete the selectors first, then the analysis script. Each part is graded separately. In addition to the five required searches, choose at least three phrases of your own. You can use the fictional slogans from sample_merchandise.html for ideas.
Extract listings
Complete the eight functions in lab_selectors.py. Their doctests specify inputs and results. Open sample_listings.html in your browser to check the records you extract. See the Beautiful Soup examples for selectors, text, and attributes.
-
Implement
count_matches,select_texts, andselect_attr, returning results in page order. Preserve text whitespace inselect_texts. -
Implement
extract_titleswith surrounding whitespace removed, andextract_prices_centswith integer prices in cents. -
Implement
count_free_shippingandfirst_link. ReturnNonewhen there is no first link. -
Implement
extract_listings: return one dictionary per item withname,price_cents, andurl, searching within that item.
Analyze the archive
Complete lab_tweets.py. Use the JSON example to load records and the bar-chart reference for plotting. The starter’s format_table.py demonstrates number formatting. All results must come from the input files, including when the grader supplies different tweets.
-
Load
master_2009.jsonthroughmaster_2018.jsonand print the combined record count. Count duplicate records as stored. -
Count tweets containing
Obama,Trump,Mexico,Russia, andFake News, ignoring capitalization. Usefull_textwhen present, otherwisetext. Count each tweet once per phrase. -
Choose at least three additional phrases and compute their counts in the same way.
-
Generate a labeled bar chart from your calculated counts or percentages. Use the standard library and matplotlib for the analysis script.
-
Put a Markdown table in
README.mdwith all eight or more phrases and their percentages. Right-align the phrase column and display percentages with two digits before and after the decimal, such as07.73. -
Write at least one sentence describing what your results show.
-
Save the chart as an image, commit it, and embed it using a local image link in the README.
Check and submit
Run:
python -m doctest lab_selectors.py
python lab_tweets.pyFor the supplied archive, check the total of 35402 tweets, 13659 containing Trump, and 2738 containing Obama. Open the saved chart and confirm that the README displays both the table and image.
Commit and sync lab_selectors.py, lab_tweets.py, README.md, and the saved chart. On Gradescope, choose GitHub and submit lab-web-data and the branch containing your work. The submission guide explains resubmission.