-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpycas2020_web_scraping.Rmd
413 lines (299 loc) · 10.7 KB
/
pycas2020_web_scraping.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
---
title: "Adventures in Babysitting: Introduction to Web Scraping in Python"
author: "Julia Piaskowski"
date: "2020/02/09"
output:
ioslides_presentation:
widescreen: true
---
<style type="text/css">
body p {
color: #282828;
}
ul {
color: #282828;
}
code {
color: ##0033cc;
}
.forceBreak { -webkit-column-break-after: always; break-after: column; }
</style>
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, error=TRUE, tidy=TRUE)
```
```{r wrap-hook, echo=FALSE}
library(knitr)
hook_output = knit_hooks$get('output')
knit_hooks$set(output = function(x, options) {
# this hook is used only when the linewidth option is not NULL
if (!is.null(n <- options$linewidth)) {
x = knitr:::split_lines(x)
# any lines wider than n should be wrapped
if (any(nchar(x) > n)) x = strwrap(x, width = n)
x = paste(x, collapse = '\n')
}
hook_output(x, options)
})
```
```{r echo=F}
library(reticulate)
virtualenv_create("pycas2020")
# a few things you might need to install
# py_install(c("requests", "json", "pandas", "DataFrame"))
# py_install(c("bs4", "Tag"))
use_virtualenv("pycas2020")
```
##
### https://github.com/jpiaskowski/pycas2020_web_scraping
## Good Way to Learn Python!
```{r, echo=FALSE, out.height='40%'}
knitr::include_graphics("images/webscraping_book.png")
```
## But, Who Actually Reads These A to Z?
(spoiler: not me)
```{r, echo=FALSE, fig.cap="me and my programming books", out.width='100%'}
knitr::include_graphics("images/luke_lightsaber_throwaway.gif")
```
## The Main Things to Know in a Web Scraping Project:
* Is it worth the trouble?
* Is it ethical?
* Tools available in `BeautifulSoup` and `requests`
* What to look for in html code
* Parsing json objects with <code>json</code>
* Rudimentary `pandas` skills
* `<pro-tip> All you need to know about html is how tags work </pro-tip>`
## What to Look for in a Scraping Project: {.columns-2}
* A sizeable amount of structured data with a regular repeatable format.
* Identical formating is not required, but the more edge cases present, the more complicated the scraping will be.
* no API available
```{r, echo=FALSE, out.width = '80%'}
knitr::include_graphics("images/spidermen.jpg")
```
## Ethics in Scraping {.columns-2}
```{r, echo=FALSE, out.width = '80%'}
knitr::include_graphics("images/captain_marvel_binary.jpg")
```
Accessing vast troves of information can be intoxicating.
*Just because it's possible doesn't mean it should be done*
## Legal Considerations
*(note: I have zero legal training - this is not legal advice!)*
* Are you scraping copyrighted material?
* Will your scraping activity compromise individual privacy?
* Are you making a large number of request that may overload or damage a server?
* Is it possible the scraping will expose intellectual property you do not own?
* Are there terms of service governing use of the website and are you following those?
* Will your scraping activities diminish the value of the original data?
## Dollar Stores are Taking Over the World!
```{r, echo=FALSE, fig.cap="Store in Cascade, Idaho", out.width='60%'}
knitr::include_graphics("images/family_dollar_cascade_cropped.png")
```
**Goal:** Extract addresses for all Family Dollar stores in Idaho.
## The Starting Point:
https://locations.familydollar.com/id/
```{r, echo=FALSE,out.width='80%'}
knitr::include_graphics("images/familydollar1.png")
```
## Step 1: Load the Libraries
```{python}
import requests # for making standard html requests
from bs4 import BeautifulSoup # magical tool for parsing html data
import json # for parsing data
from pandas import DataFrame as df # data organization
```
## Step 2: Grab Some Data from Target Web Address
```{python}
page = requests.get("https://locations.familydollar.com/id/")
soup = BeautifulSoup(page.text, 'html.parser')
```
Beautiful Soup will take html or xml content and transform it into a complex tree of objects. Here are several common types:
* `BeautifulSoup` - the soup (the parsed content)
* `Tag` - main type of bs4 element you will encounter
* `NavigableString` - string within a tag
* `Comment` - special type of NavigableString
## More on 'requests.get' output:
Different output types
* `page.text` for text
* `page.content` for byte-by-byte output
* `page.json` for json objects
* `page.raw` for the raw socket response (no thank you)
The encoding for text can be set:
* `page.encoding = 'ISO-885901'`
## More on Tags
* The bs4 element 'tag' is an html tag
* it has both a name and attributes (accessed like a dictionary)
* if a tag has multiple attritutes with the same name, only the first instance is accessed
* a tag's children is accessed via `[tag].contents`
* all tag descendenats can be accessed with `[tag].descendants`
* you can always access the full contents as a string:* `re.compile("your_string")` instead of navigating the html tree
## Step 3: Determine How to Extract Relevant Content from bs4 Soup
*This process can be frustrating.*
```{r, echo=FALSE, out.width='70%'}
knitr::include_graphics("images/ren_throws_fit.gif")
```
## Step 3: Finding Content...
* Start with one representative example and then scale up
* Viewing the page's html source code is essential
* Run at your own risk:
```{python, eval=F, echo=T}
print(soup.prettify())
```
## Step 3: Finding Content...
* It is usually easiest to browse via "View Page Source":
```{r, echo=FALSE, out.width='100%'}
knitr::include_graphics("images/familydollar2.png")
```
* What attribute or tag sets your content apart from the rest?
## Step 3: Finding Content by Searching
Searching for 'href' does not work.
```{python}
dollar_tree_list = soup.find_all('href')
dollar_tree_list
```
But searching on a specific class is often successful:
```{python}
dollar_tree_list = soup.find_all(class_ = 'itemlist')
for i in dollar_tree_list[:2]:
print(i)
```
## Step 3: Finding Target Content by Using 'contents'
```{python, collapse=TRUE}
type(dollar_tree_list)
len(dollar_tree_list)
```
Next, extract contents from this BeautifulSoup "ResultSet".
```{python}
example = dollar_tree_list[2] # Arco, ID (single representative example)
example_content = example.contents
print(example_content)
```
## Step 3: Finding Content in Attributes
Find out what attributes are present in the contents:
*Note: `contents` usually return a list of exactly one item, so the first step is to index that item.*
```{python}
example_content = example.contents[0]
example_content.attrs
```
Extract the relevant attribute:
```{python}
example_href = example_content['href']
print(example_href)
```
## Step 4: Extract the Relevant Content
```{python}
city_hrefs = [] # initialise empty list
for i in dollar_tree_list:
cont = i.contents[0]
href = cont['href']
city_hrefs.append(href)
# check to be sure all went well
for i in city_hrefs[:2]:
print(i)
```
Result: a list of URL's of Family Dollar stores in Idaho to scrape
## Repeat Steps 1-4 for the City URLs
```{python}
page2 = requests.get(city_hrefs[2]) # representative example
soup2 = BeautifulSoup(page2.text, 'html.parser')
```
```{r, echo=FALSE, out.width='100%'}
knitr::include_graphics("images/familydollar3.png")
```
## Extract Address Information
from `type="application/ld+json"`
```{python}
arco = soup2.find_all(type="application/ld+json")
print(arco[1])
```
(address information is in the second list member)
## Use 'contents' to Find Address Information
Extract the contents (from the second list item) and index the first (and only) list item:
```{python}
arco_contents = arco[1].contents[0]
arco_contents
```
Next, convert to a json object:
*(these are way easier to work with)*
```{python}
arco_json = json.loads(arco_contents)
```
## Extract Content from a json Object
A json object is a dictionary:
```{python, linewidth=85}
type(arco_json)
print(arco_json)
```
## Extract Content from a json Object
```{python, linewidth=70}
arco_address = arco_json['address']
arco_address
```
## Step 5: Put It All Together
Iterate over the list store URLs in Idaho:
```{python}
locs_dict = [] # initialise empty list
for link in city_hrefs:
locpage = requests.get(link) # request page info
locsoup = BeautifulSoup(locpage.text, 'html.parser')
# parse the page's content
locinfo = locsoup.find_all(type="application/ld+json")
# extract specific element
loccont = locinfo[1].contents[0]
# get contents from the bs4 element set
locjson = json.loads(loccont) # convert to json
locaddr = locjson['address'] # get address
locs_dict.append(locaddr) # add address to list
```
## Step 6: Finalise Data
```{python}
locs_df = df.from_records(locs_dict)
locs_df.drop(['@type', 'addressCountry'], axis = 1, inplace = True)
locs_df.head(n = 5)
```
## Results!!
```{r, echo=FALSE, out.width='70%'}
knitr::include_graphics("images/adventures_in_babysitting.gif")
```
```{python, eval=F, echo=T}
df.to_csv(locs_df, "family_dollar_ID_locations.csv", sep = ",", index = False)
```
## A Few Words on Selenium
"Inspect Element" provides the code for what is displayed in a browser.
```{r, echo=FALSE, out.width='100%'}
knitr::include_graphics("images/walgreens1.png")
```
## A Few Words on Selenium
"View Page Source" - provides the code for what `requests` will obtain
```{r, echo=FALSE, out.width='70%'}
knitr::include_graphics("images/walgreens2.png")
```
There are plugins modifying the source code - so, it should be accessed *after* the page has loaded in a browser.
## A Few Words on Selenium
* Requires a webdriver to retrieve the content
* It actually opens a web browser, and this info is collected
* Selenium is powerful - it can interact with loaded content in many ways
* After getting data, continue to use `BeautifulSoup` as before
```{python, eval=F, echo=T}
url = "https://www.walgreens.com/storelistings/storesbycity.jsp?requestType=locator&state=ID"
driver = webdriver.Firefox(executable_path = 'mypath/geckodriver.exe')
driver.get(url)
soup_ID = BeautifulSoup(driver.page_source, 'html.parser')
store_link_soup = soup_ID.find_all(class_ = 'col-xl-4 col-lg-4 col-md-4')
```
## The Penultimate Slide {.columns-2}
**Read the Manuals**
* https://beautiful-soup-4.readthedocs.io/en/latest/
* https://selenium.dev/
This talk available at:
<font size="4"> https://github.com/jpiaskowski/pycas2020_web_scraping </font >
```{r, echo=FALSE, fig.cap="Perservere", out.width='80%'}
knitr::include_graphics("images/yoda_lightsaber.gif")
```
## ~ After Becoming a Web Scraping Master ~
https://github.com/jpiaskowski/pycas2020_web_scraping
```{r, echo=FALSE, out.width='100%'}
knitr::include_graphics("images/luke_brushesoff_dust.gif")
```
## Bonus Slide!
```{r, echo=FALSE, fig.cap = "Dollar Stores in America", out.width='95%'}
knitr::include_graphics("images/family_dollar_locations.png")
```