RECSM Summer School: Scraping the web Pablo Barber a School of - - PowerPoint PPT Presentation

recsm summer school scraping the web
SMART_READER_LITE
LIVE PREVIEW

RECSM Summer School: Scraping the web Pablo Barber a School of - - PowerPoint PPT Presentation

RECSM Summer School: Scraping the web Pablo Barber a School of International Relations University of Southern California pablobarbera.com Networked Democracy Lab www.netdem.org Course website: github.com/pablobarbera/big-data-upf


slide-1
SLIDE 1

RECSM Summer School: Scraping the web

Pablo Barber´ a School of International Relations University of Southern California pablobarbera.com Networked Democracy Lab www.netdem.org Course website:

github.com/pablobarbera/big-data-upf

slide-2
SLIDE 2

Scraping the web: what? why?

An increasing amount of data is available on the web:

◮ Speeches, sentences, biographical information...

slide-3
SLIDE 3

Scraping the web: what? why?

An increasing amount of data is available on the web:

◮ Speeches, sentences, biographical information... ◮ Social media data, newspaper articles, press releases...

slide-4
SLIDE 4

Scraping the web: what? why?

An increasing amount of data is available on the web:

◮ Speeches, sentences, biographical information... ◮ Social media data, newspaper articles, press releases... ◮ Geographic information, conflict data...

slide-5
SLIDE 5

Scraping the web: what? why?

An increasing amount of data is available on the web:

◮ Speeches, sentences, biographical information... ◮ Social media data, newspaper articles, press releases... ◮ Geographic information, conflict data...

These datasets are often provided in an unstructured format.

slide-6
SLIDE 6

Scraping the web: what? why?

An increasing amount of data is available on the web:

◮ Speeches, sentences, biographical information... ◮ Social media data, newspaper articles, press releases... ◮ Geographic information, conflict data...

These datasets are often provided in an unstructured format. Web scraping is the process of extracting this information automatically and transforming it into a structured dataset.

slide-7
SLIDE 7

Scraping the web: two approaches

Two different approaches:

  • 1. Screen scraping: extract data from source code of website,

with html parser and/or regular expressions

slide-8
SLIDE 8

Scraping the web: two approaches

Two different approaches:

  • 1. Screen scraping: extract data from source code of website,

with html parser and/or regular expressions

◮ rvest package in R

slide-9
SLIDE 9

Scraping the web: two approaches

Two different approaches:

  • 1. Screen scraping: extract data from source code of website,

with html parser and/or regular expressions

◮ rvest package in R

  • 2. Web APIs (application programming interfaces): a set of

structured http requests that return JSON or XML data

slide-10
SLIDE 10

Scraping the web: two approaches

Two different approaches:

  • 1. Screen scraping: extract data from source code of website,

with html parser and/or regular expressions

◮ rvest package in R

  • 2. Web APIs (application programming interfaces): a set of

structured http requests that return JSON or XML data

◮ httr package to construct API requests

slide-11
SLIDE 11

Scraping the web: two approaches

Two different approaches:

  • 1. Screen scraping: extract data from source code of website,

with html parser and/or regular expressions

◮ rvest package in R

  • 2. Web APIs (application programming interfaces): a set of

structured http requests that return JSON or XML data

◮ httr package to construct API requests ◮ Packages specific to each API: weatherData, WDI,

Rfacebook... Check CRAN Task View on Web Technologies and Services for more examples

slide-12
SLIDE 12

The rules of the game

  • 1. Respect the hosting site’s wishes:
slide-13
SLIDE 13

The rules of the game

  • 1. Respect the hosting site’s wishes:

◮ First, check if an API exists or if data are available for

download

slide-14
SLIDE 14

The rules of the game

  • 1. Respect the hosting site’s wishes:

◮ First, check if an API exists or if data are available for

download

◮ Some websites disallow scrapers on their robots.txt

files

slide-15
SLIDE 15

The rules of the game

  • 1. Respect the hosting site’s wishes:

◮ First, check if an API exists or if data are available for

download

◮ Some websites disallow scrapers on their robots.txt

files

  • 2. Limit your bandwidth use:
slide-16
SLIDE 16

The rules of the game

  • 1. Respect the hosting site’s wishes:

◮ First, check if an API exists or if data are available for

download

◮ Some websites disallow scrapers on their robots.txt

files

  • 2. Limit your bandwidth use:

◮ Wait one or two seconds after each hit

slide-17
SLIDE 17

The rules of the game

  • 1. Respect the hosting site’s wishes:

◮ First, check if an API exists or if data are available for

download

◮ Some websites disallow scrapers on their robots.txt

files

  • 2. Limit your bandwidth use:

◮ Wait one or two seconds after each hit ◮ Scrape only what you need, and just once (e.g. store the

html file in disk, and then parse it)

slide-18
SLIDE 18

The rules of the game

  • 1. Respect the hosting site’s wishes:

◮ First, check if an API exists or if data are available for

download

◮ Some websites disallow scrapers on their robots.txt

files

  • 2. Limit your bandwidth use:

◮ Wait one or two seconds after each hit ◮ Scrape only what you need, and just once (e.g. store the

html file in disk, and then parse it)

  • 3. When using APIs, read documentation
slide-19
SLIDE 19

The rules of the game

  • 1. Respect the hosting site’s wishes:

◮ First, check if an API exists or if data are available for

download

◮ Some websites disallow scrapers on their robots.txt

files

  • 2. Limit your bandwidth use:

◮ Wait one or two seconds after each hit ◮ Scrape only what you need, and just once (e.g. store the

html file in disk, and then parse it)

  • 3. When using APIs, read documentation

◮ Is there a batch download option?

slide-20
SLIDE 20

The rules of the game

  • 1. Respect the hosting site’s wishes:

◮ First, check if an API exists or if data are available for

download

◮ Some websites disallow scrapers on their robots.txt

files

  • 2. Limit your bandwidth use:

◮ Wait one or two seconds after each hit ◮ Scrape only what you need, and just once (e.g. store the

html file in disk, and then parse it)

  • 3. When using APIs, read documentation

◮ Is there a batch download option? ◮ Are there any rate limits?

slide-21
SLIDE 21

The rules of the game

  • 1. Respect the hosting site’s wishes:

◮ First, check if an API exists or if data are available for

download

◮ Some websites disallow scrapers on their robots.txt

files

  • 2. Limit your bandwidth use:

◮ Wait one or two seconds after each hit ◮ Scrape only what you need, and just once (e.g. store the

html file in disk, and then parse it)

  • 3. When using APIs, read documentation

◮ Is there a batch download option? ◮ Are there any rate limits? ◮ Can you share the data?

slide-22
SLIDE 22

The art of web scraping

Workflow:

  • 1. Learn about structure of website
slide-23
SLIDE 23

The art of web scraping

Workflow:

  • 1. Learn about structure of website
  • 2. Build prototype code
slide-24
SLIDE 24

The art of web scraping

Workflow:

  • 1. Learn about structure of website
  • 2. Build prototype code
  • 3. Generalize: functions, loops, debugging
slide-25
SLIDE 25

The art of web scraping

Workflow:

  • 1. Learn about structure of website
  • 2. Build prototype code
  • 3. Generalize: functions, loops, debugging
  • 4. Data cleaning
slide-26
SLIDE 26

Three main scenarios

  • 1. Data in table format
slide-27
SLIDE 27

Three main scenarios

  • 2. Data in unstructured format

www.ipaidabribe.com/reports/paid

slide-28
SLIDE 28

Three main scenarios

  • 3. Data hidden behind web forms

Candidates on 2015 Venezuelan parliamentary election

slide-29
SLIDE 29

Three main scenarios

  • 1. Data in table format
slide-30
SLIDE 30

Three main scenarios

  • 1. Data in table format

◮ Automatic extraction with rvest

slide-31
SLIDE 31

Three main scenarios

  • 1. Data in table format

◮ Automatic extraction with rvest

  • 2. Data in unstructured format
slide-32
SLIDE 32

Three main scenarios

  • 1. Data in table format

◮ Automatic extraction with rvest

  • 2. Data in unstructured format

◮ Element identification with selectorGadget

slide-33
SLIDE 33

Three main scenarios

  • 1. Data in table format

◮ Automatic extraction with rvest

  • 2. Data in unstructured format

◮ Element identification with selectorGadget ◮ Automatic extraction with rvest

slide-34
SLIDE 34

Three main scenarios

  • 1. Data in table format

◮ Automatic extraction with rvest

  • 2. Data in unstructured format

◮ Element identification with selectorGadget ◮ Automatic extraction with rvest

  • 3. Data hidden behind web forms
slide-35
SLIDE 35

Three main scenarios

  • 1. Data in table format

◮ Automatic extraction with rvest

  • 2. Data in unstructured format

◮ Element identification with selectorGadget ◮ Automatic extraction with rvest

  • 3. Data hidden behind web forms

◮ Automation of web browser behavior with selenium

slide-36
SLIDE 36

APIs

API = Application Programming Interface; a set of structured https requests that return data in JSON or XML format. Types of APIs:

  • 1. RESTful APIs: queries for static information at current

moment (e.g. user profiles, posts, etc.)

  • 2. Streaming APIs: changes in users’ data in real time (e.g.

new tweets, new FB posts...)

slide-37
SLIDE 37

APIs

API = Application Programming Interface; a set of structured https requests that return data in JSON or XML format. Types of APIs:

  • 1. RESTful APIs: queries for static information at current

moment (e.g. user profiles, posts, etc.)

  • 2. Streaming APIs: changes in users’ data in real time (e.g.

new tweets, new FB posts...) Most APIs are rate-limited:

◮ Restrictions on number of API calls by user/IP address and

period of time.

slide-38
SLIDE 38

Connecting with an API

Constructing a REST API call:

◮ Baseline URL: https://maps.googleapis.com/maps/api/geocode/json ◮ Parameters: ?address=barcelona ◮ Authentication token: &key=XXXXX

Response is often in JSON format.

slide-39
SLIDE 39

Connecting with an API

Constructing a REST API call:

◮ Baseline URL: https://maps.googleapis.com/maps/api/geocode/json ◮ Parameters: ?address=barcelona ◮ Authentication token: &key=XXXXX

Response is often in JSON format. Authentication:

◮ Many APIs require an access key or token ◮ An alternative, open standard is called OAuth ◮ Connections without sharing username or password, only

temporary tokens that can be refreshed

◮ httr package in R implements most cases (examples)

slide-40
SLIDE 40

Other APIs

See CRAN Web Technologies Task View