6 Lesson 06: Webscraping
6.1 Getting to Know WGET
Here we will discuss webscraping — the main process of efficiently collecting large volumes of information from the Internet.
6.2 Software
wget(https://www.gnu.org/software/wget/), a free software package for retrieving files using HTTP, HTTPS, FTP and FTPS the most widely-used Internet protocols. It is a non-interactive command line tool, so it may easily be called from scripts, cron jobs, terminals without X-Windows support, etc.NB: on installing
wget:- On Windows (the easiest): download from https://eternallybored.org/misc/wget/ > choose the latest 64-bit ZIP file (
EXEwill most likely be blocked by your browser as a potentially dangerous file).- Unzip the file and copy
wget.exeto the folder where you are planning to scrape data; NB: the easiest approach on Windows is to move/copy this file into relevant folders.
- Unzip the file and copy
- On Mac (and, possibly, Linux):
brew install wget
- On Windows (the easiest): download from https://eternallybored.org/misc/wget/ > choose the latest 64-bit ZIP file (
6.3 Class
- practical examples of working with
wget - single link download
- batch download
- web-page analysis
- extraction of links with
regular expressions - modification of links with
regular expressions
6.4 Sample commands
wget link
wget -i file_with_links.txt
wget -i file_with_links.txt -P ./folderYouWantToSaveTo/ -nc
Where:
-Pis a folder parameter, which instructswgetwhere you want to store downloaded files (optional).-ncis a no-clobber parameter, which instructswgetto skips files, if they already exist (optional)
Link examples:
https://maximromanov.github.io/dh_in_mes/files/articles/1860-11-12_article_01.txt
https://maximromanov.github.io/dh_in_mes/files/articles/1860-11-12_article_02.txt
https://maximromanov.github.io/dh_in_mes/files/articles/1860-11-12_article_03.txt
NB: there are many other parameters with which you can adjust wget to your needs.
6.4.1 Issues with WGET on Windows
As we have run into a number of issues with trying to run WGET on Windows, here are some steps that will help to run it smoothly:
- first of all,
WGETdoes not seem to play well withPowershell; it does work without any problems viaCommand Prompt(if your Windows “speaks” German, it is called Eingabeaufforderung; but if you search forCommand Prompt, you should still be able to find it.). It also works without any issues viaGit-Bash. The following steps will make it easier to use with bothCommand PromptandGit-Bash. - download the
wget.exefile (from here: https://eternallybored.org/misc/wget/); - copy/paste it into the
C:\Windows\System32folder (C:\Windows\System32is a part of the so-calledPATH— a series of paths which all Windows command line tools check); - now, all the commands should be working as expected;
NB: There is an alternative to WGET on Windows Powershell. Here is a detailed tutorial: https://adamtheautomator.com/powershell-download-file/.
6.4.2 Issues with WGET on Mac
It may so happen that WGET will not work as intended with the homework assignment (specifically, you may not be able to download all the issues of Dispatch). From what I understand this is specifically a Mac issue. Last year the MacOS changed their main command line program from bash to zsh, and in some cases WGET may not work as intended under zsh. The solution is, luckily, rather simple: we just need to install bash and run WGET from bash.
- to install
bash, runbrew install bash; - after that you can start
bashby runningbashon the command line; the prompt (the beginning of the command line where you type in your commands) should change into something like:bash-5.1$ - now you can go to a folder where you want to save your downloaded results and run
WGET; - NB: you will need
bashonly for this step; after you restart theTerminal, you will be back to the defaultzsh(you can also runexitcommand to quitbashand return tozsh).
6.5 A sidenote on issues in general
Keep in mind that one of the most important things that you need to learn in this course is that there are multiple solutions to most of the problems and tasks that you may face and the most common way to solve your problem is to break is down into smaller tasks (remember, there was a detailed discussion of this in Zelle’s book), and then look for efficient solutions to each step. No matter how advanced you are, “googling” will be the major way of finding suitable solutions.
6.6 Practicing Scraping
The following sections give you some examples of links that you are most likely to encounter in real life. Your task is to figure out how to prepare lists of links (URLs) for downloading with WGET. Your first step will be to look under the hood of the current page, which you can do right clicking on the page and selecting something that looks like “View page source” (in Chrome or Edge) or “Show Page Source” (in Safari; you will need to enable “Show Develop menu in menu bar” in Preferences > Advanced). Now, looking at the HTML code of the page you can find the actual URLs, which you can then extract from the HTML code with regular expressions (one thing you can do is to copy/paste the entire code of the page into a text editor that supports regular expressions—like Sublime Text).
6.7 Practice 1: very easy
6.8 Practice 2: easy-ish
6.9 Practice 3 (aka Homework): a tiny-bit tricky
- download issues of “Richmond Times Dispatch” (Years 1860-1865, only!), which are available at: http://www.perseus.tufts.edu/hopper/collection?collection=Perseus:collection:RichTimes)
6.10 Reference Materials
- Milligan, Ian. 2012. “Automated Downloading with Wget.” Programming Historian, June. https://programminghistorian.org/lessons/automated-downloading-with-wget.
- Kurschinski, Kellen. 2013. “Applied Archival Downloading with Wget.” Programming Historian, September. https://programminghistorian.org/lessons/applied-archival-downloading-with-wget.
- Baxter, Richard. 2019. “How to download your website using WGET for Windows.” https://builtvisible.com/download-your-website-with-wget/.
- Alternatively, this operation can be done with a Python script: Turkel, William J., and Adam Crymble. 2012. “Downloading Web Pages with Python.” Programming Historian, July. https://programminghistorian.org/lessons/working-with-web-pages.
6.11 Homework
- Scraping the “Dispatch”: download issues of “Richmond Times Dispatch” (Years 1860-1865, only!), which are available at: http://www.perseus.tufts.edu/hopper/collection?collection=Perseus:collection:RichTimes)
- In a separate markdown file, describe your steps of how you completed this task (to be uloaded with the rest of your homework).
Python
- Work through Chapters 8 and 11 of Zelle’s book; read chapters carefully; work through the chapter summaries and exercises; complete the following programming exercises: 1-8 in Chapter 8 and 1-11 in Chapter 11;
- Watch Dr. Vierthaler’s videos:
- Episode 12: Functions
- Episode 13: Libraries and NLTK
- Episode 14: Regular Expressions
- Note: the sequences are somewhat different in Zelle’s textbook and Vierthaler’s videos. I would recommend you to always check Vierthaler’s videos and also check videos which cover topics that you read about in Zelle’s book.
6.12 Submitting homework
- Homework assignment must be submitted by the beginning of the next class;
- Email your homework to the instructor.
- if your homework is to create a file, email it as an attachment
- if your homework is a blogpost on your website, email the link to your website and to the blogpost with your homework.
- In the subject of your email, please, add the following:
070112-LXX-HW-YourLastName-YourMatriculationNumber, whereLXXis the lesson for which the homework is submitted,YourLastNameis your last name, andYourMatriculationNumberis your matriculation number.