## 9.1
library('httr')
library('rvest')
response <- GET('http://codingsocialscience.org/')
website_content <- content( response , 'text' )
parsed_website <- read_html( website_content )
for ( node in html_nodes( parsed_website , 'li' ) ) {
print( html_text(node) )
}
No encoding supplied: defaulting to UTF-8.
[1] "\r\n HOME (current)\r\n " [1] "\r\n Review book now\r\n " [1] "The blended approach used in this book reflects my teaching and situates computational methods in social research, providing both ideas on research design and hands-on tools to implement such research." [1] "It covers both top-level research design and more detailed approaches on how to implement and conduct research using computational methods." [1] "By exploring some key traditions in computer science, it helps readers to understand computational approaches and problems from a social science perspective." [1] "Situate computational social science research to fully appreciate its interdisciplinary nature, and understand the core challenges and benefits that computational methods provide for social sciences." [1] "Understand how computational processes work through improving their computational thinking. They need a short introduction to programming to help \"translate\" a research problem into an algorithm, and to understand how algorithms work." [1] "Gain familiarity with different method ‘families’ (data science, network analysis, simulation models, constructive work) to help understand the range of application domains and possibilities of computational methods." [1] "Situating Computational Social Science" [1] "Programming and computational thinking" [1] "Algorithmic data analysis" [1] "Network analysis" [1] "Simulations and complex systems" [1] "Constructing interactive systems" [1] "Data structures" [1] "Best practices for software development" [1] "Research ethics adn Computational Social Science" [1] "Validity, reliability and computational social sciences" [1] "Integrating Computational Methods in a Social Science Research" [1] "Introduction to computational social science" [1] "Programming for social scientists" [1] "Data Science for social scientists" [1] "Politics of Human-Computer Interaction."
## 9.2
library('httr')
library('rvest')
response <- GET('https://uk.sagepub.com/')
website_content <- content( response , 'text' )
parsed_website <- read_html( website_content )
image_count <- 0
for ( node in html_nodes( parsed_website , 'img' ) ) {
image_count <- image_count + 1
}
print( image_count )
[1] 5
## 9.3
library('httr')
library('rvest')
response <- GET('https://www.helsinki.fi/')
website_content <- content( response , 'text' )
website_content <- tolower( website_content )
search_words = list( 'Twitter', 'Facebook', 'YouTube' )
for( search_word in search_words ) {
if( grepl( tolower(search_word), website_content ) ) {
print( paste( "Found", search_word) )
}
}
[1] "Found Twitter" [1] "Found Facebook" [1] "Found YouTube"
## 9.4
library('httr')
library('rvest')
library('stringr')
universities <- list('https://www.helsinki.fi', 'https://www.aalto.fi')
results = list()
search_words = list( 'Twitter', 'Facebook', 'YouTube' )
for( university in universities ){
response <- GET(university)
website_content <- content( response , 'text' )
website_content <- tolower( website_content )
res <- list()
for( search_word in search_words ) {
matches <- str_count( website_content, tolower(search_word) )
res[[ search_word ]] <- matches
}
results[[ university ]] <- res
}
print( results )
$`https://www.helsinki.fi` $`https://www.helsinki.fi`$Twitter [1] 6 $`https://www.helsinki.fi`$Facebook [1] 4 $`https://www.helsinki.fi`$YouTube [1] 3 $`https://www.aalto.fi` $`https://www.aalto.fi`$Twitter [1] 10 $`https://www.aalto.fi`$Facebook [1] 5 $`https://www.aalto.fi`$YouTube [1] 5
## 9.5
if( FALSE ) { ## this code could not be automatically tested, so comment out
library('httr')
library('rvest')
start_url <- "http://codingsocialscience.org/"
start_page <- GET(start_url)
start_page <- read_html( start_page )
for( link in html_nodes( start_page , 'a' ) ) {
link <- html_attr( link , 'href')
if( startsWith( link, 'http' ) ) {
followup <- GET( link )
print( link )
followup <- read_html( followup )
for( followuplink in html_nodes( followup, 'a' ) ) {
followuplink <- html_attr( followuplink, 'href' )
## some site does not define href on their link, this takes care of that issue
## followuplink <- ifelse( is.na( followuplink ) , '' , followuplink )
if( startsWith( followuplink , 'http') ) {
print( paste( link, '-', followuplink ) )
}
}
}
}
}
## 9.6
library('httr')
library('jsonlite')
url <- 'https://data.police.uk/api/crimes-street/all-crime?lat=51.5073&lng=-0.171505' ## latest month is shown by default, see documentation
data <- GET(url)
data <- content( data, 'text' )
data <- fromJSON( data ) ## could just be URL, but we do a bit step-by-step here
categories = list() ## this is a megacollector
for( rn in 1:nrow(data) ) {
category <- data[rn, 'category']
if( ! category %in% names( categories) ) {
categories[[ category ]] <- 0
}
categories[[ category ]] <- categories[[ category ]] + 1
}
print( categories )
No encoding supplied: defaulting to UTF-8.
$`anti-social-behaviour` [1] 237 $`bicycle-theft` [1] 29 $burglary [1] 98 $`criminal-damage-arson` [1] 48 $drugs [1] 56 $`other-theft` [1] 400 $`possession-of-weapons` [1] 4 $`public-order` [1] 102 $robbery [1] 74 $shoplifting [1] 86 $`theft-from-the-person` [1] 247 $`vehicle-crime` [1] 116 $`violent-crime` [1] 258 $`other-crime` [1] 8
## 9.7
library('httr')
library('jsonlite')
url <- 'http://api.worldbank.org/v2/country/fi?format=json' ## latest month is shown by default, see documentation
data <- GET(url)
data <- content( data, 'text' )
data <- fromJSON( data ) ## could just be URL, but we do a bit step-by-step here
print( data[[2]][1, 'capitalCity'] )
[1] "Helsinki"
## 9.8
library('httr')
library('jsonlite')
adjacents <- list('se', 'no', 'ru', 'ee')
for( country in adjacents ) {
url <- paste( 'http://api.worldbank.org/v2/country/', country, '?format=json' , sep = '' )
data <- GET(url)
data <- content( data, 'text' )
data <- fromJSON( data ) ## could just be URL, but we do a bit step-by-step here
print( data[[2]][1, 'capitalCity'] )
}
[1] "Stockholm" [1] "Oslo" [1] "Moscow" [1] "Tallinn"
## 9.9
library('httr')
library('jsonlite')
url <- 'http://api.worldbank.org/v2/country/all?format=json'
data <- GET(url)
data <- content( data, 'text' )
data <- fromJSON( data ) ## could just be URL, but we do a bit step-by-step here
## the book does not cover pagination, so let's this is not a full result
data = data[[2]] ## index 1 is for pagination details
for( rn in 1:nrow(data) ) {
print( paste( data[rn, 'latitude'], data[rn, 'longitude'], data[rn, 'incomeLevel']['value'] ) )
}
[1] "12.5167 -70.0167 High income" [1] " Aggregates" [1] "34.5228 69.1761 Low income" [1] " Aggregates" [1] " Aggregates" [1] "-8.81155 13.242 Lower middle income" [1] "41.3317 19.8172 Upper middle income" [1] "42.5075 1.5218 High income" [1] " Aggregates" [1] "24.4764 54.3705 High income" [1] "-34.6118 -58.4173 Upper middle income" [1] "40.1596 44.509 Upper middle income" [1] "-14.2846 -170.691 Upper middle income" [1] "17.1175 -61.8456 High income" [1] "-35.282 149.129 High income" [1] "48.2201 16.3798 High income" [1] "40.3834 49.8932 Upper middle income" [1] "-3.3784 29.3639 Low income" [1] " Aggregates" [1] " Aggregates" [1] "50.8371 4.36761 High income" [1] "6.4779 2.6323 Lower middle income" [1] "12.3605 -1.53395 Low income" [1] "23.7055 90.4113 Lower middle income" [1] "42.7105 23.3238 Upper middle income" [1] " Aggregates" [1] "26.1921 50.5354 High income" [1] "25.0661 -77.339 High income" [1] "43.8607 18.4214 Upper middle income" [1] " Aggregates" [1] "53.9678 27.5766 Upper middle income" [1] "17.2534 -88.7713 Upper middle income" [1] " Aggregates" [1] "32.3293 -64.706 High income" [1] "-13.9908 -66.1936 Lower middle income" [1] "-15.7801 -47.9292 Upper middle income" [1] "13.0935 -59.6105 High income" [1] "4.94199 114.946 High income" [1] " Aggregates" [1] "27.5768 89.6177 Lower middle income" [1] "-24.6544 25.9201 Upper middle income" [1] " Aggregates" [1] "5.63056 21.6407 Low income" [1] "45.4215 -75.6919 High income" [1] " Aggregates" [1] " Aggregates" [1] " Aggregates" [1] "46.948 7.44821 High income" [1] " High income" [1] "-33.475 -70.6475 High income"
## 9.10
example <- '127.0.0.1 - - [10/Nov/2020:08:43:18 +0200] "GET / HTTP/1.1" 200 4471 "-" "Mozilla/5.0"'
example <- strsplit(example, '\\[' )[[1]][2] ## everything to right from [
example <- strsplit(example, '\\]' )[[1]][1] ## everything to left from ]
## 10/Nov/2020:08:43:18 +0200 # you can print it out here
## print( example )
example <- strsplit( example, ':')[[1]][1] ## everything left from :
date <- strsplit( example, '/')[[1]]
day <- date[1]
month <- date[2]
year <- date[3]
print( paste( year, month, day ) )
[1] "2020 Nov 10"
## 9.11
library('readtext')
all_text <- readtext('alice.pdf')
all_text <- all_text[['text']]
print( all_text )
[1] "the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink\neyes ran close by her.\n There was nothing so VERY remarkable in that; nor did Alice think it so VERY much\nout of the way to hear the Rabbit say to itself, 'Oh dear! Oh dear! I shall be late!' (when she\nthought it over afterwards, it occurred to her that she ought to have wondered at this, but at\nthe time it all seemed quite natural); but when the Rabbit actually TOOK A WATCH OUT\nOF ITS WAISTCOAT-POCKET, and looked at it, and then hurried on, Alice started to her\nfeet, for it flashed across her mind that she had never before seen a rabbit with either a\nwaistcoat-pocket, or a watch to take out of it, and burning with curiosity, she ran across the\nfield after it, and fortunately was just in time to see it pop down a large rabbit-hole under\nthe hedge.\n In another moment down went Alice after it, never once considering how in the world\nshe was to get out again.\n The rabbit-hole went straight on like a tunnel for some way, and then dipped suddenly\ndown, so suddenly that Alice had not a moment to think about stopping herself before she\nfound herself falling down a very deep well.\n Either the well was very deep, or she fell very slowly, for she had plenty of time as she\nwent down to look about her and to wonder what was going to happen next. First, she tried\nto look down and make out what she was coming to, but it was too dark to see anything;\nthen she looked at the sides of the well, and noticed that they were filled with cupboards\nand book-shelves; here and there she saw maps and pictures hung upon pegs. She took\ndown a jar from one of the shelves as she passed; it was labelled 'ORANGE\nMARMALADE', but to her great disappointment it was empty: she did not like to drop the\njar for fear of killing somebody, so managed to put it into one of the cupboards as she fell\npast it.\n 'Well!' thought Alice to herself, 'after such a fall as this, I shall think nothing of tumbling\ndown stairs! How brave they'll all think me at home! Why, I wouldn't say anything about it,\neven if I fell off the top of the house!' (Which was very likely true.)\n Down, down, down. Would the fall NEVER come to an end! 'I wonder how many miles\nI've fallen by this time?' she said aloud. 'I must be getting somewhere near the centre of the\nearth. Let me see: that would be four thousand miles down, I think—' (for, you see, Alice\nhad learnt several things of this sort in her lessons in the schoolroom, and though this was\nnot a VERY good opportunity for showing off her knowledge, as there was no one to listen\nto her, still it was good practice to say it over) '—yes, that's about the right distance—but\nthen I wonder what Latitude or Longitude I've got to?' (Alice had no idea what Latitude\nwas, or Longitude either, but thought they were nice grand words to say.)\n Presently she began again. 'I wonder if I shall fall right THROUGH the earth! How funny\nit'll seem to come out among the people that walk with their heads downward! The\nAntipathies, I think—' (she was rather glad there WAS no one listening, this time, as it\ndidn't sound at all the right word) '—but I shall have to ask them what the name of the\ncountry is, you know. Please, Ma'am, is this New Zealand or Australia?' (and she tried to\ncurtsey as she spoke—fancy CURTSEYING as you're falling through the air! Do you think\n\nyou could manage it?) 'And what an ignorant little girl she'll think me for asking! No, it'll\nnever do to ask: perhaps I shall see it written up somewhere.'\n Down, down, down. There was nothing else to do, so Alice soon began talking again.\n'Dinah'll miss me very much to-night, I should think!' (Dinah was the cat.) 'I hope they'll\nremember her saucer of milk at tea-time. Dinah my dear! I wish you were down here with\nme! There are no mice in the air, I'm afraid, but you might catch a bat, and that's very like a\nmouse, you know. But do cats eat bats, I wonder?' And here Alice began to get rather\nsleepy, and went on saying to herself, in a dreamy sort of way, 'Do cats eat bats? Do cats eat\nbats?' and sometimes, 'Do bats eat cats?' for, you see, as she couldn't answer either question,\nit didn't much matter which way she put it. She felt that she was dozing off, and had just\nbegun to dream that she was walking hand in hand with Dinah, and saying to her very\nearnestly, 'Now, Dinah, tell me the truth: did you ever eat a bat?' when suddenly, thump!\nthump! down she came upon a heap of sticks and dry leaves, and the fall was over.\n Alice was not a bit hurt, and she jumped up on to her feet in a moment: she looked up,\nbut it was all dark overhead; before her was another long passage, and the White Rabbit was\nstill in sight, hurrying down it. There was not a moment to be lost: away went Alice like the\nwind, and was just in time to hear it say, as it turned a corner, 'Oh my ears and whiskers,\nhow late it's getting!' She was close behind it when she turned the corner, but the Rabbit\nwas no longer to be seen: she found herself in a long, low hall, which was lit up by a row of\nlamps hanging from the roof.\n There were doors all round the hall, but they were all locked; and when Alice had been\nall the way down one side and up the other, trying every door, she walked sadly down the\nmiddle, wondering how she was ever to get out again.\n Suddenly she came upon a little three-legged table, all made of solid glass; there was\nnothing on it except a tiny golden key, and Alice's first thought was that it might belong to\none of the doors of the hall; but, alas! either the locks were too large, or the key was too\nsmall, but at any rate it would not open any of them. However, on the second time round,\nshe came upon a low curtain she had not noticed before, and behind it was a little door\nabout fifteen inches high: she tried the little golden key in the lock, and to her great delight\nit fitted!\n Alice opened the door and found that it led into a small passage, not much larger than a\nrat-hole: she knelt down and looked along the passage into the loveliest garden you ever\nsaw. How she longed to get out of that dark hall, and wander about among those beds of\nbright flowers and those cool fountains, but she could not even get her head through the\ndoorway; 'and even if my head would go through,' thought poor Alice, 'it would be of very\nlittle use without my shoulders. Oh, how I wish I could shut up like a telescope! I think I\ncould, if I only know how to begin.' For, you see, so many out-of-the-way things had\nhappened lately, that Alice had begun to think that very few things indeed were really\nimpossible.\n There seemed to be no use in waiting by the little door, so she went back to the table, half\nhoping she might find another key on it, or at any rate a book of rules for shutting people up\nlike telescopes: this time she found a little bottle on it, ('which certainly was not here\n"
## 9.12
library('readtext')
all_text <- readtext('alice.docx')
all_text <- all_text[['text']]
print( all_text )
[1] "the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her.\nThere was nothing so VERY remarkable in that; nor did Alice think it so VERY much out of the way to hear the Rabbit say to itself, 'Oh dear! Oh dear! I shall be late!' (when she thought it over afterwards, it occurred to her that she ought to have wondered at this, but at the time it all seemed quite natural); but when the Rabbit actually TOOK A WATCH OUT OF ITS WAISTCOAT-POCKET, and looked at it, and then hurried on, Alice started to her feet, for it flashed across her mind that she had never before seen a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity, she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit-hole under the hedge.\nIn another moment down went Alice after it, never once considering how in the world she was to get out again.\nThe rabbit-hole went straight on like a tunnel for some way, and then dipped suddenly down, so suddenly that Alice had not a moment to think about stopping herself before she found herself falling down a very deep well.\nEither the well was very deep, or she fell very slowly, for she had plenty of time as she went down to look about her and to wonder what was going to happen next. First, she tried to look down and make out what she was coming to, but it was too dark to see anything; then she looked at the sides of the well, and noticed that they were filled with cupboards and book-shelves; here and there she saw maps and pictures hung upon pegs. She took down a jar from one of the shelves as she passed; it was labelled 'ORANGE MARMALADE', but to her great disappointment it was empty: she did not like to drop the jar for fear of killing somebody, so managed to put it into one of the cupboards as she fell past it.\n'Well!' thought Alice to herself, 'after such a fall as this, I shall think nothing of tumbling down stairs! How brave they'll all think me at home! Why, I wouldn't say anything about it, even if I fell off the top of the house!' (Which was very likely true.)\nDown, down, down. Would the fall NEVER come to an end! 'I wonder how many miles I've fallen by this time?' she said aloud. 'I must be getting somewhere near the centre of the earth. Let me see: that would be four thousand miles down, I think—' (for, you see, Alice had learnt several things of this sort in her lessons in the schoolroom, and though this was not a VERY good opportunity for showing off her knowledge, as there was no one to listen to her, still it was good practice to say it over) '—yes, that's about the right distance—but then I wonder what Latitude or Longitude I've got to?' (Alice had no idea what Latitude was, or Longitude either, but thought they were nice grand words to say.)\nPresently she began again. 'I wonder if I shall fall right THROUGH the earth! How funny it'll seem to come out among the people that walk with their heads downward! The Antipathies, I think—' (she was rather glad there WAS no one listening, this time, as it didn't sound at all the right word) '—but I shall have to ask them what the name of the country is, you know. Please, Ma'am, is this New Zealand or Australia?' (and she tried to curtsey as she spoke—fancy CURTSEYING as you're falling through the air! Do you think\nyou could manage it?) 'And what an ignorant little girl she'll think me for asking! No, it'll never do to ask: perhaps I shall see it written up somewhere.'\nDown, down, down. There was nothing else to do, so Alice soon began talking again. 'Dinah'll miss me very much to-night, I should think!' (Dinah was the cat.) 'I hope they'll remember her saucer of milk at tea-time. Dinah my dear! I wish you were down here with me! There are no mice in the air, I'm afraid, but you might catch a bat, and that's very like a mouse, you know. But do cats eat bats, I wonder?' And here Alice began to get rather sleepy, and went on saying to herself, in a dreamy sort of way, 'Do cats eat bats? Do cats eat bats?' and sometimes, 'Do bats eat cats?' for, you see, as she couldn't answer either question, it didn't much matter which way she put it. She felt that she was dozing off, and had just begun to dream that she was walking hand in hand with Dinah, and saying to her very earnestly, 'Now, Dinah, tell me the truth: did you ever eat a bat?' when suddenly, thump! thump! down she came upon a heap of sticks and dry leaves, and the fall was over.\nAlice was not a bit hurt, and she jumped up on to her feet in a moment: she looked up, but it was all dark overhead; before her was another long passage, and the White Rabbit was still in sight, hurrying down it. There was not a moment to be lost: away went Alice like the wind, and was just in time to hear it say, as it turned a corner, 'Oh my ears and whiskers, how late it's getting!' She was close behind it when she turned the corner, but the Rabbit was no longer to be seen: she found herself in a long, low hall, which was lit up by a row of lamps hanging from the roof.\nThere were doors all round the hall, but they were all locked; and when Alice had been all the way down one side and up the other, trying every door, she walked sadly down the middle, wondering how she was ever to get out again.\nSuddenly she came upon a little three-legged table, all made of solid glass; there was nothing on it except a tiny golden key, and Alice's first thought was that it might belong to one of the doors of the hall; but, alas! either the locks were too large, or the key was too small, but at any rate it would not open any of them. However, on the second time round, she came upon a low curtain she had not noticed before, and behind it was a little door about fifteen inches high: she tried the little golden key in the lock, and to her great delight it fitted!\nAlice opened the door and found that it led into a small passage, not much larger than a rat-hole: she knelt down and looked along the passage into the loveliest garden you ever saw. How she longed to get out of that dark hall, and wander about among those beds of bright flowers and those cool fountains, but she could not even get her head through the doorway; 'and even if my head would go through,' thought poor Alice, 'it would be of very little use without my shoulders. Oh, how I wish I could shut up like a telescope! I think I could, if I only know how to begin.' For, you see, so many out-of-the-way things had happened lately, that Alice had begun to think that very few things indeed were really impossible.\nThere seemed to be no use in waiting by the little door, so she went back to the table, half hoping she might find another key on it, or at any rate a book of rules for shutting people up like telescopes: this time she found a little bottle on it, ('which certainly was not here"
## 9.13
library('httr')
library('rvest')
library('stringr')
library('jsonlite')
universities <- list('https://www.helsinki.fi', 'https://www.aalto.fi')
results = list()
search_words = list( 'Twitter', 'Facebook', 'YouTube' )
for( university in universities ){
response <- GET(university)
website_content <- content( response , 'text' )
website_content <- tolower( website_content )
res <- list()
for( search_word in search_words ) {
matches <- str_count( website_content, tolower(search_word) )
res[[ search_word ]] <- matches
}
results[[ university ]] <- res
}
write( toJSON( results, auto_unbox = TRUE ), file = 'universities.json' ) ## auto_unbox ensures that the single values are not stored as lists, more common way of presenting these in JSON
## 9.14
library('jsonlite')
data <- read_json("universities.json")
for( university in names( data ) ) {
results <- data[[university]]
counts <- 0
for( count in unlist( results ) ) {
counts <- counts + count
}
print( paste( university, counts ) )
}
[1] "https://www.helsinki.fi 13" [1] "https://www.aalto.fi 20"
## 9.15
library('jsonlite')
## this could also be a dictionary in dictionary
northwest <- list()
northeast <- list()
southwest <- list()
southeast <- list()
data <- read_json( "countries.json" )
for( country in data ) {
income <- country[['income']]
if( country[['lat']] > 0 ) {
if( country[['long']] > 0 ) {
if( ! income %in% names( northeast ) ) {
northeast[[income]] <- 0
}
northeast[[income]] <- northeast[[income]] + 1
} else {
if( ! income %in% names( northwest ) ) {
northwest[[income]] <- 0
}
northwest[[income]] <- northwest[[income]] + 1
}
} else {
if( country[['long']] > 0 ) {
if( ! income %in% names( southeast ) ) {
southeast[[income]] <- 0
}
southeast[[income]] <- southeast[[income]] + 1
} else {
if( ! income %in% names( southwest ) ) {
southwest[[income]] <- 0
}
southwest[[income]] <- southwest[[income]] + 1
}
}
}
print( paste( northwest, northeast, southwest, southeast ) )
[1] "6 2 3 1" "1 6 1 1" "1 7 1 1" "6 3 3 1"
## 9.16
library('jsonlite')
url = 'https://data.police.uk/api/crimes-street/all-crime?lat=51.5073&lng=-0.171505'
dataframe <- fromJSON( url )
print( table( dataframe$category ) )
anti-social-behaviour bicycle-theft burglary 237 29 98 criminal-damage-arson drugs other-crime 48 56 8 other-theft possession-of-weapons public-order 400 4 102 robbery shoplifting theft-from-the-person 74 86 247 vehicle-crime violent-crime 116 258