全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
1235 2
2014-12-21
Automated Data Collection with R 第一二章的代码烦的一笔,花了一天的时间对代码进行了优化。


library(rvest)
library(RCurl)

#Generate empty folder
dir.create("Bills_111/")

# Iterate over all 4059 pieces of legislation
for(i in 1:4059){
    # Generate the unique URL for each piece of legislation
    url <- str_c(
        "http://thomas.loc.gov/cgi-bin/bdquery/D?d111:",
        i,
        ":./list/bss/d111SN.lst:@@@P"
    )
    # Download the page
    bill.result <- getURL(url)
    # Write the page to local hard drive
    write(
        bill.result, str_c(
            "Bills_111/Bill_111_S",
            i,
            ".html"
        )
    )
    # Print progress of download
    cat(i, "\n")
}


#简化部分

myfun = function(i) {
             url = str_c("Bills_111/Bill_111_S", i, ".html")
             sponsors = url %>% html() %>% html_nodes("div a") %>% html_text() %>% .[[15]] %>% gsub("Sen","",.)
             cosponsors = url %>% html() %>% html_nodes("p a") %>% html_text() %>% .[-1] %>% gsub("Sen","",.)
             list(sponsors,cosponsors)
            }

sponsor.list = lapply(1:4059,myfun)

sponsor.list[[1]][1] = " Reid, Harry"

all.senators = sponsor.list %>% unlist %>% unique %>% sort %>% .[-length(.)] #109个参议员

# Create a matrix of sponsors

sponsor.matrix = matrix(NA, nrow = 4059, ncol = length(all.senators))
colnames(sponsor.matrix) = all.senators
rownames(sponsor.matrix) = paste("S.", seq(1, 4059), sep ="")

for(i in 1:length(sponsor.list)){
    sponsor.matrix[i, which(all.senators == sponsor.list[[i]][1])] = "Sponsor"
    sponsor.matrix[i, which(all.senators %in% unlist(sponsor.list[[i]][2]))] = "Cosponsor"
    }

> sponsor.matrix[30:35,31:34]
      Cornyn, John  Crapo, Mike  DeMint, Jim  Dodd, Christopher J.
S.30 NA            NA           NA           NA                  
S.31 NA            NA           NA           NA                  
S.32 NA            NA           NA           NA                  
S.33 NA            NA           NA           NA                  
S.34 "Cosponsor"   "Cosponsor"  "Sponsor"    NA                  
S.35 "Cosponsor"   NA           NA           NA  



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2014-12-31 09:23:34
这个应该怎么运行?
我是说怎么把这些代码放在哪?通过什么途径实现数据的自动收集
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2015-2-9 05:41:10
提示: 作者被禁止或删除 内容自动屏蔽
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群