Scraping for Journalists (2nd edition)
Scraping for Journalists (2nd edition)
How to grab information from hundreds of sources, put it in data you can interrogate - and still hit deadlines
About the Book
Scraping - getting a computer to capture information from online sources - is one of the most powerful techniques for data-savvy journalists who want to get to the story first, or find exclusives that no one else has spotted. Faster than FOI and more detailed than advanced search techniques, scraping also allows you to grab data that organisations would rather you didn’t have - and put it into a form that allows you to get answers.
Scraping for Journalists introduces you to a range of scraping techniques - from very simple scraping techniques which are no more complicated than a spreadsheet formula, to more complex challenges such as scraping databases or hundreds of documents. At every stage you'll see results - but you'll also be building towards more ambitious and powerful tools.
You’ll be scraping within 5 minutes of reading the first chapter - but more importantly you'll be learning key principles and techniques for dealing with scraping problems.
Unlike general books about programming languages, everything in this book has a direct application for journalism, and each principle of programming is related to their application in scraping for newsgathering. And unlike standalone guides and blog posts that cover particular tools or techniques, this book aims to give you skills that you can apply in new situations and with new tools.
Bundles that include this book
Table of Contents
-
1. Introduction
- Your own robot
- A book about not reading books
- I’m not a programmer
- PS: This isn’t a book
-
2. Scraper #1: Start scraping in 5 minutes
- How it works: functions and parameters
- What are the parameters? Strings and indexes
- Tables and lists?
- Recap
- Tests
-
3. Scraper #2: What happens when the data isn’t in a table?
- Strong structure: XML
- Scraping XML
- Recap
- Tests
-
4. Scraper #3: Looking for structure in HTML
- Detour: Introduction to HTML and the LIFO rule
- Attributes and values
- Classifying sections of content: div, span, classes and ids
- Back to Scraper #3: Scraping a <div> in a HTML webpage
- Recap
- Tests
-
5. Scraper #4: Finding more structure in webpages: Xpath
- Recap
- Tests
-
6. Scraper #5: Scraping multiple pages with Google Drive
- Recap
- Tests
-
7. Scraper #6: Structure in URLs - using Open Refine
- Looking for structure in URLs
- Assembling the ingredients
- Grabbing ID codes or URLs from a website
- Using Open Refine as a scraper
- Grabbing the HTML for each page
-
Extracting data from the raw HTML with
parseHTML
- Using CSS selectors in scraping
- Understanding the results
- Recap
- Tests
-
8. Scraper #7: Scraping multiple pages with ‘next’ links using Outwit Hub
- Creating a basic scraper in OutWit Hub
- Scraping a series of pages
- Customised scrapers in OutWit
- Trying it out on a search for questions about health
- Recap
- Tests
-
9. Scraper #8: Poorly formatted webpages - solving problems with OutWit
- Identifying what structure there is
- Repeating a heading or other piece of data for each part within it
- Splitting a larger piece of data into bits: using separators
- Recap
- Tests
-
10. Scraper #9: Scraping uglier HTML and ‘regular expressions’ in an OutWit scraper
- Introducing Regex
- Using regex to specify a range of possible matches
- Catching the regular expression too
- I want any character: the wildcard and quantifiers
- Matching zero, one or more characters - quantifiers
- 3 questions: What characters, how many, where?
- Using regex on an ugly page
- What’s the pattern?
- Matching non-textual characters
- What if my data contains full stops, forward slashes or other special characters?
- ‘Anything but that!’ - negative matches
- This or that - looking for more than one regular expression at the same time
- Only here - specifying location
- Back to the scraper: grabbing the rest of the data
- Which dash? Negative matches in practice.
- Recap
- Tests
-
11. Scrapers #10 and #11: Scraping hidden and ‘invisible’ data on a webpage: icons and ‘reveals’
- Scraping accessibility data on Olympic venues
- Hidden HTML
- Recap
- Tests
-
12. Scraper #12: An introduction to Python: adapting scraper code
- Python - you already know some of it
- The ScraperWiki Classic Archive
- Forking a scraper
- Introducing Morph.io
- Finding a scraper to clone
- How to copy a scraper into Morph.io
- Adapting the code
- Recap
- Tests
-
13. Scraper #13: Tracing the code - libraries and functions, and documentation in Scraperwiki
- Parent/child relationships
- Parameters (again)
- Detour: Variables
- Back to Scraper #9
- Recap
- Tests
-
14. Scraper #13 continued: Scraperwiki’s tutorial scraper 2
- What are those variables?
- Detour: loops (for and while)
- Back to scraper #13: Storing the data
- Detour: Unique keys, primary keys, and databases
- A unique key can’t be empty: fixing the error
- Summing up the scraper
- Recap
- Tests
-
15. Scraper #14: Adapting the code to scrape a different webpage
- Dealing with errors
- Recap
- Tests
-
16. Scraper #15: Scraping multiple cells and pages
-
Creating your own functions:
def
- If statements - asking a question
- Numbers in square brackets? Indexes again!
- Attributes
- Recap
- Tests
-
Creating your own functions:
-
17. Scraper #16: Adapting your third scraper: creating more than one column of data
- Recap
- Tests
-
18. Scraper #17: Scraping a list of pages
- Iterating
-
Tip: creating a list of items using the
JOIN
function in a spreadsheet - Recap
- Tests
-
19. Scraper #18: Scraping a page - and the pages linked (badly) from it
- Using ranges to avoid errors
-
Using
len
to test lists - Other workarounds
- Recap
- Scraper tip: a checklist for understanding someone else’s code
-
20. Scraper #19: Scraping scattered data from multiple websites that share the same CMS
- Finding websites using the same content management system (CMS)
- Writing the scraper: looking at HTML structure
- Recap
- Tests
-
21. Scraper #20: Automating database searches (forms)
- Understanding URLs: queries and parameters
- When the URL doesn’t change
- Solving the cookie problem: Mechanize
- Recap
- Tests
-
22. Scraper #21: Storing the results of a search
- Recap
-
Scraper tip: using
print
to monitor progress - Tests
-
23. Scraper #22: Scraping PDFs part 1
- Detour: indexes and slicing shortcuts
- Back to the scraper
- Detour: operators
- Back to the scraper (again)
- Detour: the % sign explained
- Back to the scraper (again) (again)
- Running the code on a working URL
- Borrowing some code
- The first test run
- Fixing a unicode error
- Where’s the ‘view source’ on a PDF?
- Recap
- Tests
-
24. Scraper 23: Scraping PDFs part 2
- Scraping speed camera PDFs - welcome back to XPath
- Ifs and buts: measuring and matching data
- Recap
- Tests
-
25. Scraper 24: Scraping multiple PDFs
- The code
- Tasks 1 and 2: Find a pattern in the HTML and grab the links within
- XPath contains…
- The code: scraping more than one PDF
- The wrong kind of data: calculations with strings
- Putting square pegs in square holes: saving data based on properties
- Recap
- Tests
-
26. Scraper 25: Text, not tables, in PDFs - regex
- Starting the code: importing a regex library
- Scraping each PDF
- The UnicodeDecodeError and the AttributeError
- Storing the first pieces of information
- Why it’s a good idea to store line numbers
-
Re
: Python’s regex library -
Other functions from the
re
library - Back to the code
- Joining lists of items into a single string
- Finding all the links to PDF reports on a particular webpage
- Finding the PDF link on a page
- Detour: global variables and local variables
- The code in full
- Recap
- Tests
-
27. Scraper 26: Scraping CSV files
- The CSV library
- Process of elimination 1: putting blind spots in the code
- Process of elimination 2: amending the source data
- Encoding, decoding, extracting
- Removing the header row
- Ready to scrape multiple sheets
- Combining CSV files on your computer
- Recap
- Tests
-
28. Scraper 27: Scraping Excel spreadsheets part 1
- A library for scraping spreadsheets
- What can you learn from a broken scraper?
- Applying the scraper to a different spreadsheet
- But what is the scraper doing?
- Recap
- Tests
-
29. Scraper 28: Scraping Excel spreadsheets part 2: scraping one sheet
- Testing on one sheet of a spreadsheet
- Recap
- Tests
-
30. Scraper 28 continued: Scraping Excel spreadsheets part 3: scraping multiple sheets
- One dataset, or multiple ones
- Using header row values as keys
- Recap
- Tests
-
31. Scraper 28 continued: Scraping Excel spreadsheets part 4: Dealing with dates in spreadsheets
- Scraping multiple spreadsheets
- Loops within loops
- Scraper tip: creating a sandbox
- Recap
- Tests
-
32. Scraper 29: writing scrapers for JSON and APIs
- If you’re API and you know it
- Dealing with JSON
- Adding our own code
- Storing the results
- Querying your own API
- Recap
- Tests
-
33. The final chapter: where do you go from here?
- The map is not the territory
- Recommended reading and viewing
- End != End
- 34. Acknowledgements
- 35. Glossary
Other books by this author
The Leanpub 60-day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms
80% Royalties. Earn $16 on a $20 book.
We pay 80% royalties. That's not a typo: you earn $16 on a $20 sale. If we sell 5000 non-refunded copies of your book or course for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earned$12,307,240writing, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them
Top Books
OpenIntro Statistics
David Diez, Christopher Barr, Mine Cetinkaya-Rundel, and OpenIntroA complete foundation for Statistics, also serving as a foundation for Data Science.
Leanpub revenue supports OpenIntro (US-based nonprofit) so we can provide free desk copies to teachers interested in using OpenIntro Statistics in the classroom and expand the project to support free textbooks in other subjects.
More resources: openintro.org.
Personal Finance
Jason AndersonThis textbook provides an in-depth analysis on personal finance that is both practical and straightforward in its approach. It has been written in such a way that the readers can gain knowledge without getting overwhelmed by the technical terms. Suitable for both beginners and advanced learners.
Getting to Know IntelliJ IDEA
Trisha Gee and Helen ScottIf we treat our IDE as a text editor, we are doing ourselves a disservice. Using a combination of tutorials and a questions-and-answers approach, Getting to Know IntelliJ IDEA will help you find ways to use IntelliJ IDEA that enable you to work comfortably and productively as a professional developer.
C++20 - The Complete Guide
Nicolai M. JosuttisAll new language and library features of C++20 (for those who know previous C++ versions).
The book presents all new language and library features of C++20. Learn how this impacts day-to-day programming, to benefit in practice, to combine new features, and to avoid all new traps.
Buy early, pay less, free updates.
Other books:
Mastering STM32 - Second Edition
Carmine NovielloWith more than 1200 microcontrollers, STM32 is probably the most complete ARM Cortex-M platform on the market. This book aims to be the most complete guide around introducing the reader to this exciting MCU portfolio from ST Microelectronics and its official CubeHAL and STM32CubeIDE development environment.
R Programming for Data Science
Roger D. PengThis book brings the fundamentals of R programming to you, using the same material developed as part of the industry-leading Johns Hopkins Data Science Specialization. The skills taught in this book will lay the foundation for you to begin your journey learning data science. Printed copies of this book are available through Lulu.
Machine Learning Q and AI
Sebastian Raschka, PhDHave you recently completed a machine learning or deep learning course and wondered what to learn next? With 30 questions and answers on key concepts in machine learning and AI, this book provides bite-sized bits of knowledge for your journey to becoming a machine learning expert.
Stats One
William FooteThe Rails 7 Way
Obie Fernandez, Lucas Dohmen, and Tom Henrik AadlandThe Rails™ 7 Way is the comprehensive, authoritative reference guide for professionals delivering production-quality code using modern Ruby on Rails. It illuminates the entire Rails 7 API, its most powerful idioms, design approaches, and libraries. Building on the previous editions, this edition has been heavily refactored and updated.
Gradual Modularization for Ruby and Rails
Stephan HagemannGet yourself a new tool to manage your Rails application and your growing engineering organization! Prevent the ball-of-mud (and fix it!). Go for microservices or SOA if it makes sense not just because you don't have any other tool. Do all this through a low-overhead tool: packages. Enable better conversations to make practical changes today.
Top Bundles
- #1
Software Architecture
2 Books
"Software Architecture for Developers" is a practical and pragmatic guide to modern, lightweight software architecture, specifically aimed at developers. You'll learn:The essence of software architecture.Why the software architecture role should include coding, coaching and collaboration.The things that you really need to think about before... - #2
CCIE Service Provider Ultimate Study Bundle
2 Books
Piotr Jablonski, Lukasz Bromirski, and Nick Russo have joined forces to deliver the only CCIE Service Provider training resource you'll ever need. This bundle contains a detailed and challenging collection of workbook labs, plus an extensively detailed technical reference guide. All of us have earned the CCIE Service Provider certification... - #3
1500 QUIZ COMMENTATI (3 libri)
3 Books
Tre libri dei QUIZ MMG Commentati al prezzo di DUE! I QUIZ dei concorsi ufficiali di Medicina Generale relativi agli anni: 2000-2001-2003-2012-2013-2014-2015-2016-2017-2018-2019-2020-2021 +100 inediti Raccolti in unico bundle per aiutarvi nello studio e nella preparazione al concorso. All'interno di ogni libro i quiz sono stati suddivisi per... - #4
Pattern-Oriented Memory Forensics and Malware Detection
2 Books
This training bundle for security engineers and researchers, malware and memory forensics analysts includes two accelerated training courses for Windows memory dump analysis using WinDbg. It is also useful for technical support and escalation engineers who analyze memory dumps from complex software environments and need to check for possible... - #5
Practical FP in Scala + Functional event-driven architecture
2 Books
Practical FP in Scala (A hands-on approach) & Functional event-driven architecture, aka FEDA, (Powered by Scala 3), together as a bundle! The content of PFP in Scala is a requirement to understand FEDA so why not take advantage of this bundle!? - #6
Modern C++ Collection
3 Books
Get All about Modern C++C++ Standard Library, including C++20Concurrency with Modern C++, including C++20C++20Each book has about 200 complete code examples. Updates are included. When I update one of the books, you immediately get the updated bundle. You can expect significant updates to each new C++ standard (C++23, C++26, .. ) and also... - #7
Linux Administration Complet
4 Books
Ce lot comprend les quatre volumes du Guide Linux Administration :Linux Administration, Volume 1, Administration fondamentale : Guide pratique de préparation aux examens de certification LPIC 1, Linux Essentials, RHCSA et LFCS. Administration fondamentale. Introduction à Linux. Le Shell. Traitement du texte. Arborescence de fichiers. Sécurité... - #9
Development and Deployment of Multiplayer Online Games, Part ARCH. Architecture (Vol. I-III)
3 Books
What's the Big Idea? The idea behind this book is to summarize the body of knowledge that already exists on multiplayer games but is not available in one single place.And quite a fewof the issues discussed within this series (planned as three nine volumes ~300 pages each), while known in the industry, have not been published at all (except for... - #10
Growing Agile: The Complete Coach's Guide
7 Books
Growing Agile: Coach's Guide Series This bundle provides a collection of training and workshop plans for a variety of agile topics. The series is aimed at agile coaches, trainers and ScrumMasters who often find themselves needing to help teams understand agile concepts. Each book in the series provides the plans, slides, handouts and activity...