-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_headers_and_cookies.R
67 lines (57 loc) · 1.78 KB
/
demo_headers_and_cookies.R
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
if(!require(dplyr)) {
install.packages("dplyr", repos="http://cran.us.r-project.org")
require(dplyr)
}
if(!require(rvest)) {
install.packages("rvest", repos="http://cran.us.r-project.org")
require(rvest)
}
if(!require(httr)) {
install.packages("httr", repos="http://cran.us.r-project.org")
require(httr)
}
if(!require(purrr)) {
install.packages("purrr", repos="http://cran.us.r-project.org")
require(purrr)
}
if(!require(jsonlite)) {
install.packages("jsonlite", repos="http://cran.us.r-project.org")
require(jsonlite)
}
# screen scraping not possible here:
base_url <- 'https://httpbin.org'
# add a custom header
GET(glue::glue('{base_url}/headers'), add_headers('X-README' = 'Hello, my name is Timo')) %>%
content()
# modify a header
GET(glue::glue('{base_url}/headers'), add_headers('X-Amzn-Trace-Id' = 'Foobar')) %>%
content()
# overwrite UA
GET(glue::glue('{base_url}/headers'), user_agent('TOR Browser')) %>%
content()
# send a cookie
GET(glue::glue('{base_url}/headers'), set_cookies('foo' = 'bar', 'x' = 'y')) %>%
content()
# store a cookie
cookie <- GET(glue::glue('{base_url}/cookies/set'),
query = list(testkey = 'testvalue')) %>%
cookies()
cookie
# cookies are automatically persisted between requests to the same domain
GET(glue::glue('{base_url}')) %>%
cookies()
# globally setting headers
set_config(
# set the user agent header
user_agent(
'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0'
)
)
# user agent for some reason does not get applied to read_html...
read_html(glue::glue('{base_url}/headers')) %>%
html_elements('p') %>%
html_text(trim = T)
# workaround: use GET() inside read_html()
read_html(GET(glue::glue('{base_url}/headers'))) %>%
html_elements('p') %>%
html_text(trim = T)