25. Title
imdb %>%
html_nodes(".lister-item-content h3 a") %>%
html_text() -> movie_title
movie_title
## [1] "The Shawshank Redemption"
## [2] "The Godfather"
## [3] "The Dark Knight"
## [4] "The Godfather: Part II"
## [5] "The Lord of the Rings: The Return of the King"
## [6] "Pulp Fiction"
## [7] "Schindler's List"
## [8] "Il buono, il brutto, il cattivo"
## [9] "12 Angry Men"
## [10] "Inception"
## [11] "Fight Club"
## [12] "The Lord of the Rings: The Fellowship of the Ring"
## [13] "Forrest Gump"
## [14] "The Lord of the Rings: The Two Towers"
## [15] "The Matrix"
## [16] "Goodfellas"
## [17] "Star Wars: Episode V - The Empire Strikes Back"
25
29. Certificate
imdb %>%
html_nodes(".lister-item-content p .certificate") %>%
html_text() -> movie_certificate
movie_certificate
## [1] "A" "A" "UA" "PG-13" "A" "A" "UA" "A"
## [9] "PG-13" "PG-13" "PG-13" "A" "A" "PG" "UA" "R"
## [17] "PG" "A" "A" "PG-13" "A" "R" "A" "A"
## [25] "U" "PG" "UA" "U" "U" "UA" "A" "UA"
## [33] "PG-13" "A" "R" "R" "R" "A" "U" "U"
## [41] "R" "U" "PG" "R"
29
45. List of Governors
rbi_guv %>%
html_nodes("table") %>%
html_table() %>%
extract2(2) -> profile
profile
## No. Officeholder Portrait Term start Term
## 1 1 Osborne Smith NA 1 April 1935 30 June 1
## 2 2 James Braid Taylor NA 1 July 1937 17 February 1
## 3 3 C. D. Deshmukh NA 11 August 1943ii 30 May 1
## 4 4 Benegal Rama Rau NA 1 July 1949 14 January 1
## 5 5 K. G. Ambegaonkar NA 14 January 1957 28 February 1
## 6 6 H. V. R. Iyengar NA 1 March 1957 28 February 1
## 7 7 P. C. Bhattacharya NA 1 March 1962 30 June 1
## 8 8 Lakshmi Kant Jha NA 1 July 1967 3 May 1
## 9 9 B. N. Adarkar NA 4 May 1970 15 June 1
## 10 10 Sarukkai Jagannathan NA 16 June 1970 19 May 1
## 11 11 N. C. Sen Gupta NA 19 May 1975 19 August 1
## 12 12 K. R. Puri NA 20 August 1975 2 May 1
## 13 13 M. Narasimham NA 3 May 1977 30 November 1
## 14 14 I. G. Patel NA 1 December 1977 15 September 1
## 15 15 Manmohan Singh NA 16 September 1982 14 January 1 45
46. Sort
profile %>%
separate(`Term in office`, into = c("term", "days")) %>%
select(Officeholder, term) %>%
arrange(desc(as.numeric(term)))
## Officeholder term
## 1 Benegal Rama Rau 2754
## 2 C. D. Deshmukh 2150
## 3 R. N. Malhotra 2147
## 4 Bimal Jalan 2114
## 5 James Braid Taylor 2057
## 6 P. C. Bhattacharya 1947
## 7 Y. Venugopal Reddy 1826
## 8 H. V. R. Iyengar 1825
## 9 D. Subbarao 1825
## 10 Sarukkai Jagannathan 1798
## 11 C. Rangarajan 1795
## 12 I. G. Patel 1749
## 13 Raghuram Rajan 1096
## 14 Lakshmi Kant Jha 1037
## 15 Urjit Patel 947
## 16 Manmohan Singh 851
46
47. Backgrounds
profile %>%
count(Background)
## # A tibble: 9 x 2
## Background n
## <chr> <int>
## 1 "" 1
## 2 Banker 2
## 3 Career Reserve Bank of India officer 1
## 4 Economist 7
## 5 IAS officer 4
## 6 ICS officer 7
## 7 Indian Administrative Service (IAS) officer 1
## 8 Indian Audit and Accounts Service officer 1
## 9 Indian Civil Service (ICS) officer 1
47
48. Backgrounds
profile %>%
pull(Background) %>%
fct_collapse(
Bureaucrats = c("IAS officer", "ICS officer",
"Indian Administrative Service (IAS) officer",
"Indian Audit and Accounts Service officer",
"Indian Civil Service (ICS) officer"),
`No Info` = c(""),
`RBI Officer` = c("Career Reserve Bank of India officer")
) %>%
fct_count() %>%
rename(background = f, count = n) -> backgrounds
48
49. Backgrounds
backgrounds
## # A tibble: 5 x 2
## background count
## <fct> <int>
## 1 No Info 1
## 2 Banker 2
## 3 RBI Officer 1
## 4 Economist 7
## 5 Bureaucrats 14
49
52. Summary
web scraping is the extraction of data from web sites
best for static & well structured HTML pages
review robots.txt file
HTML code can change any time
if API is available, please use it
do not overwhelm websites with requests
•
•
•
•
•
•
52