Leanpub Header

Skip to main content

Scraping for Journalists (2nd edition)

How to grab information from hundreds of sources, put it in data you can interrogate - and still hit deadlines

Learn how to grab information from dozens or even hundreds of webpages, documents or spreadsheets using a range of tools and techniques, from simple web-based tools to full on programming.

The author is letting you choose the price you pay for this book!

Pick Your Price...
PDF
EPUB
WEB
1,638
Readers
272
Pages
89,066Words
About

About

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.

Share this book

Categories

Price

Pick Your Price...

Minimum price

$15.10

$20.01

You pay

$20.01

Author earns

$16.00
$

All prices are in US $. You can pay in US $ or in your local currency when you check out.

EU customers: prices exclude VAT, which is added during checkout.

...Or Buy With Credits!

Number of credits (Minimum 2)

2
The author will earn $24.00 from your purchase!
You can get credits monthly with a Reader Membership

Author

About the Author

Paul Bradshaw

Paul Bradshaw runs the MA in Data Journalism and the MA in Multiplatform and Mobile Journalism at Birmingham City University, where he is an associate professor. He publishes the Online Journalism Blog, and is the founder of investigative journalism website HelpMeInvestigate. He has written for the Guardian and Telegraph’s data blogs, journalism.co.uk, Press Gazette, InPublishing, Nieman Reports and the Poynter Institute in the US. Formerly Visiting Professor at City University’s School of Journalism in London, He is the author of the Online Journalism Handbook, now in its second edition, Magazine Editing (3rd Edition) with John Morrish and Mobile-First Journalism with Steve Hill. Other books which Bradshaw has contributed to include Investigative Journalism (second edition), Web Journalism: A New Form of Citizenship; and Citizen Journalism: Global Perspectives.

His books on Leanpub include Scraping for JournalistsFinding Stories in Spreadsheets, the Data Journalism Heist, Snapchat for Journalists, and 8000 Holes: How the 2012 Olympic Torch Relay Lost its Way.

Bradshaw has been listed in Journalism.co.uk’s list of the leading innovators in journalism and media and Poynter’s most influential people in social media. In 2010, he was shortlisted for Multimedia Publisher of the Year. In 2016 he was part of a team that won the CNN MultiChoice African Journalist Awards.

In addition to teaching and writing, Paul acts as a consultant and trainer to a number of organisations on social media and data journalism. You can find him on Twitter @paulbradshaw

Leanpub Podcast

Episode 11

An Interview with Paul Bradshaw

Translations

Translations

Contents

Table of Contents

1.Introduction

  1. Your own robot
  2. A book about not reading books
  3. I’m not a programmer
  4. PS: This isn’t a book

2.Scraper #1: Start scraping in 5 minutes

  1. How it works: functions and parameters
  2. What are the parameters? Strings and indexes
  3. Tables and lists?
  4. Recap
  5. Tests

3.Scraper #2: What happens when the data isn’t in a table?

  1. Strong structure: XML
  2. Scraping XML
  3. Recap
  4. Tests

4.Scraper #3: Looking for structure in HTML

  1. Detour: Introduction to HTML and the LIFO rule
  2. Attributes and values
  3. Classifying sections of content: div, span, classes and ids
  4. Back to Scraper #3: Scraping a <div> in a HTML webpage
  5. Recap
  6. Tests

5.Scraper #4: Finding more structure in webpages: Xpath

  1. Recap
  2. Tests

6.Scraper #5: Scraping multiple pages with Google Drive

  1. Recap
  2. Tests

7.Scraper #6: Structure in URLs - using Open Refine

  1. Looking for structure in URLs
  2. Assembling the ingredients
  3. Grabbing ID codes or URLs from a website
  4. Using Open Refine as a scraper
  5. Grabbing the HTML for each page
  6. Extracting data from the raw HTML with parseHTML
  7. Using CSS selectors in scraping
  8. Understanding the results
  9. Recap
  10. Tests

8.Scraper #7: Scraping multiple pages with ‘next’ links using Outwit Hub

  1. Creating a basic scraper in OutWit Hub
  2. Scraping a series of pages
  3. Customised scrapers in OutWit
  4. Trying it out on a search for questions about health
  5. Recap
  6. Tests

9.Scraper #8: Poorly formatted webpages - solving problems with OutWit

  1. Identifying what structure there is
  2. Repeating a heading or other piece of data for each part within it
  3. Splitting a larger piece of data into bits: using separators
  4. Recap
  5. Tests

10.Scraper #9: Scraping uglier HTML and ‘regular expressions’ in an OutWit scraper

  1. Introducing Regex
  2. Using regex to specify a range of possible matches
  3. Catching the regular expression too
  4. I want any character: the wildcard and quantifiers
  5. Matching zero, one or more characters - quantifiers
  6. 3 questions: What characters, how many, where?
  7. Using regex on an ugly page
  8. What’s the pattern?
  9. Matching non-textual characters
  10. What if my data contains full stops, forward slashes or other special characters?
  11. ‘Anything but that!’ - negative matches
  12. This or that - looking for more than one regular expression at the same time
  13. Only here - specifying location
  14. Back to the scraper: grabbing the rest of the data
  15. Which dash? Negative matches in practice.
  16. Recap
  17. Tests

11.Scrapers #10 and #11: Scraping hidden and ‘invisible’ data on a webpage: icons and ‘reveals’

  1. Scraping accessibility data on Olympic venues
  2. Hidden HTML
  3. Recap
  4. Tests

12.Scraper #12: An introduction to Python: adapting scraper code

  1. Python - you already know some of it
  2. The ScraperWiki Classic Archive
  3. Forking a scraper
  4. Introducing Morph.io
  5. Finding a scraper to clone
  6. How to copy a scraper into Morph.io
  7. Adapting the code
  8. How to copy a scraper into QuickCode
  9. Recap
  10. Tests

13.Scraper #13: Tracing the code - libraries and functions, and documentation in Scraperwiki

  1. Parent/child relationships
  2. Parameters (again)
  3. Detour: Variables
  4. Scraping tip #2: read code from right to left
  5. Back to Scraper #9
  6. Recap
  7. Tests

14.Scraper #13 continued: Scraperwiki’s tutorial scraper 2

  1. Scraping tip #3: follow the variables
  2. What are those variables?
  3. Detour: loops (for and while)
  4. Back to scraper #13: Storing the data
  5. Detour: Unique keys, primary keys, and databases
  6. A unique key can’t be empty: fixing the error
  7. Summing up the scraper
  8. Recap
  9. Tests

15.Scraper #14: Adapting the code to scrape a different webpage

  1. Dealing with errors
  2. Recap
  3. Tests

16.Scraper #15: Scraping multiple cells and pages

  1. Colour coding
  2. Creating your own functions: def
  3. If statements - asking a question
  4. Numbers in square brackets? Indexes again!
  5. Attributes
  6. scraperwiki.datastore generates an error
  7. Scraping tip #4: follow the functions
  8. Scraping tip #5: Read from bottom to top
  9. Recap
  10. Tests

17.Scraper #16: Adapting your third scraper: creating more than one column of data

  1. Recap
  2. Tests

18.Scraper #17: Scraping a list of pages

  1. Iterating
  2. Tip: creating a list of items using the JOIN function in a spreadsheet
  3. Recap
  4. Tests

19.Scraper #18: Scraping a page - and the pages linked (badly) from it

  1. Using ranges to avoid errors
  2. Using len to test lists
  3. Problems with URLs
  4. Methods for changing text
  5. A second way of fixing bad URLs
  6. Other workarounds
  7. Recap
  8. Scraper tip: a checklist for understanding someone else’s code

20.Scraper #19: Scraping scattered data from multiple websites that share the same CMS

  1. Finding websites using the same content management system (CMS)
  2. Writing the scraper: looking at HTML structure
  3. Using if statements to avoid errors when data doesn’t exist
  4. Recap
  5. Tests

21.Scraper #20: Automating database searches (forms)

  1. Understanding URLs: queries and parameters
  2. When the URL doesn’t change
  3. Solving the cookie problem: Mechanize
  4. Recap
  5. Tests

22.Scraper #21: Storing the results of a search

  1. Recap
  2. Scraper tip: using print to monitor progress
  3. Tests

23.Scraper #22: Scraping PDFs part 1

  1. Detour: indexes and slicing shortcuts
  2. Back to the scraper
  3. Detour: operators
  4. Back to the scraper (again)
  5. Detour: the % sign explained
  6. Back to the scraper (again) (again)
  7. Running the code on a working URL
  8. Borrowing some code
  9. The first test run
  10. Fixing a unicode error
  11. Where’s the ‘view source’ on a PDF?
  12. Recap
  13. Tests

24.Scraper 23: Scraping PDFs part 2

  1. Scraping speed camera PDFs - welcome back to XPath
  2. Ifs and buts: measuring and matching data
  3. Recap
  4. Tests

25.Scraper 24: Scraping multiple PDFs

  1. The code
  2. Tasks 1 and 2: Find a pattern in the HTML and grab the links within
  3. XPath contains…
  4. The code: scraping more than one PDF
  5. The wrong kind of data: calculations with strings
  6. Putting square pegs in square holes: saving data based on properties
  7. Recap
  8. Tests

26.Scraper 25: Text, not tables, in PDFs - regex

  1. Starting the code: importing a regex library
  2. Scraping each PDF
  3. The UnicodeDecodeError and the AttributeError
  4. Storing the first pieces of information
  5. Why it’s a good idea to store line numbers
  6. Re: Python’s regex library
  7. Other functions from the re library
  8. Back to the code
  9. Joining lists of items into a single string
  10. Finding all the links to PDF reports on a particular webpage
  11. Finding the PDF link on a page
  12. Detour: global variables and local variables
  13. The code in full
  14. Recap
  15. Tests

27.Scraper 26: Scraping CSV files

  1. The CSV library
  2. Process of elimination 1: putting blind spots in the code
  3. Process of elimination 2: amending the source data
  4. Encoding, decoding, extracting
  5. Removing the header row
  6. Ready to scrape multiple sheets
  7. Combining CSV files on your computer
  8. Recap
  9. Tests

28.Scraper 27: Scraping Excel spreadsheets part 1

  1. A library for scraping spreadsheets
  2. What can you learn from a broken scraper?
  3. Applying the scraper to a different spreadsheet
  4. But what is the scraper doing?
  5. Recap
  6. Tests

29.Scraper 28: Scraping Excel spreadsheets part 2: scraping one sheet

  1. Testing on one sheet of a spreadsheet
  2. Recap
  3. Tests

30.Scraper 28 continued: Scraping Excel spreadsheets part 3: scraping multiple sheets

  1. One dataset, or multiple ones
  2. Using header row values as keys
  3. Recap
  4. Tests

31.Scraper 28 continued: Scraping Excel spreadsheets part 4: Dealing with dates in spreadsheets

  1. More string formatting: replacing bad characters
  2. Scraping multiple spreadsheets
  3. Loops within loops
  4. Scraper tip: creating a sandbox
  5. Recap
  6. Tests

32.Scraper 29: writing scrapers for JSON and APIs

  1. If you’re API and you know it
  2. Dealing with JSON
  3. Adding our own code
  4. Storing the results
  5. Querying your own API
  6. Recap
  7. Tests

33.The final chapter: where do you go from here?

  1. The map is not the territory
  2. Recommended reading and viewing
  3. End != End

34.Acknowledgements

35.Glossary

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.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, 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

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub