replace rcurl with rvest
This commit is contained in:
+2
-55
@@ -1,6 +1,6 @@
|
|||||||
source("../utils/helpers.R")
|
source("../utils/helpers.R")
|
||||||
source("config.R")
|
source("config.R")
|
||||||
library(RCurl)
|
library(rvest)
|
||||||
library(stringr)
|
library(stringr)
|
||||||
|
|
||||||
mk_absolute_url <- function(path) paste0("https://www.bundestag.de", path)
|
mk_absolute_url <- function(path) paste0("https://www.bundestag.de", path)
|
||||||
@@ -17,7 +17,7 @@ download_protocol <- function(path, name) {
|
|||||||
|
|
||||||
fetch_batch <- function(offset) {
|
fetch_batch <- function(offset) {
|
||||||
url <- mk_url(offset)
|
url <- mk_url(offset)
|
||||||
res <- getURL(url)
|
res <- as.character(read_html(url))
|
||||||
paths <- str_match_all(res, "/resource/blob/.*?/([0-9]*-data\\.xml)")[[1]]
|
paths <- str_match_all(res, "/resource/blob/.*?/([0-9]*-data\\.xml)")[[1]]
|
||||||
mapply(download_protocol, paths[,1], paths[,2])
|
mapply(download_protocol, paths[,1], paths[,2])
|
||||||
return(length(paths) > 0)
|
return(length(paths) > 0)
|
||||||
@@ -30,56 +30,3 @@ fetch_all <- function() {
|
|||||||
offset <- 0
|
offset <- 0
|
||||||
while(fetch_batch(offset)) offset <- offset + 10
|
while(fetch_batch(offset)) offset <- offset + 10
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#______________________________________________________________________________
|
|
||||||
|
|
||||||
#Auf diese Weise funktioniert das Programm bei mir, da kein RCurl mehr
|
|
||||||
#verwendet wird. Es ist allerdings nicht mehr so schön und die Idee dazu hat
|
|
||||||
#mir Daniela gegeben. Im Grunde ist es sehr ähnlich zu dem Live-Blatt L04.
|
|
||||||
|
|
||||||
#Das Programm gibt mir eine Liste von allen Protokollen der 19. Wahlperiode
|
|
||||||
#zurück (falls man return(all_protocols) entkommentiert).
|
|
||||||
|
|
||||||
#Ich verstehe noch nicht so richtig wie man die Protokolle dann abspeichert
|
|
||||||
#(die for-Schleife zum Download läuft deswegen noch nicht ganz, sollte aber
|
|
||||||
#relativ leicht zum laufen gebracht werden, wenn man weiß, wie die Dateien
|
|
||||||
#abzuspeichern sind).
|
|
||||||
|
|
||||||
source("../utils/helpers.R")
|
|
||||||
source("config.R")
|
|
||||||
library(stringr)
|
|
||||||
library("xml2")
|
|
||||||
library(rvest)
|
|
||||||
|
|
||||||
mk_absolute_url <- function(path) paste0("https://www.bundestag.de", path)
|
|
||||||
|
|
||||||
mk_url <- function(offset) {
|
|
||||||
mk_absolute_url %$% sprintf("/ajax/filterlist/de/services/opendata/543410-543410?offset=%d",
|
|
||||||
offset)
|
|
||||||
}
|
|
||||||
|
|
||||||
read <- function(offset){
|
|
||||||
read_html(mk_url(offset))
|
|
||||||
}
|
|
||||||
|
|
||||||
fetch_batch <- function(offset){
|
|
||||||
read(offset) %>%
|
|
||||||
html_elements("a") %>%
|
|
||||||
html_attr("href") ->
|
|
||||||
paths
|
|
||||||
protocols <- mk_absolute_url(paths)
|
|
||||||
all_protocols <<- c(all_protocols, protocols)
|
|
||||||
return(length(paths) > 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
fetch_all <- function(){
|
|
||||||
all_protocols <<- c()
|
|
||||||
offset <- 0
|
|
||||||
while(fetch_batch(offset)) offset <- offset + 10
|
|
||||||
for(i in 1:length(all_protocols)){
|
|
||||||
download.file(all_protocols[i], paste0(DOWNLOAD_DIR, i)) #cannot open destfile '../data/1', reason 'No such file or directory'
|
|
||||||
#verstehe noch nicht so richtig unter welchem Pfad ich das abspeichern muss
|
|
||||||
}
|
|
||||||
# return(all_protocols)
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user