【问题标题】:Web scraping in R using for loop使用 for 循环在 R 中进行 Web 抓取
【发布时间】:2019-02-20 02:31:10
【问题描述】:

我想从this link 中抓取数据,为此我在 R 中编写了以下代码。但是,这不起作用,只返回结果的第一页。显然,循环不起作用。有人知道循环有什么问题吗?

library('rvest')

for (i in 1:40) {

     webpage <- read_html(paste0(("http://search.beaconforfreedom.org/search/censored_publications/result.html?author=&cauthor=&title=&country=7327&language=&censored_year=&censortype=&published_year=&censorreason=&sort=t&page=, i"))

     rank_data_html <- html_nodes(webpage,'tr+ tr td:nth-child(1)')

     rank_data <- html_text(rank_data_html)

     rank_data<-as.numeric(rank_data)

     title_data_html <- html_nodes(webpage,'.censo_list font')

     title_data <- html_text(title_data_html)

     author_data_html <- html_nodes(webpage,'.censo_list+ td font')
     author_data <- html_text(author_data_html)

     country_data_html <- html_nodes(webpage,'.censo_list~ td:nth-child(4) font')

     rcountry_data <- html_text(country_data_html)

     year_data_html <- html_nodes(webpage,'tr+ tr td:nth-child(5) font')

     year_data <- html_text(year_data_html)

     type_data_html <- html_nodes(webpage,'tr+ tr td:nth-child(6) font')

     type_data <- html_text(type_data_html)

}

censorship_df<-data.frame(Rank = rank_data, Title = title_data, Author = author_data, Country = rcountry_data, Type = type_data, Year = year_data)

write.table(censorship_df, file="sample.csv",sep=",",row.names=F)

【问题讨论】:

  • its robots.txt 上禁止抓取该页面
  • 您在循环的每次迭代中都重写了变量。

标签: r loops web-scraping


【解决方案1】:

你确定循环有什么问题吗?我希望它能够获得 40 次结果的第一页。看看

webpage <- read_html(paste0(("http://search.beaconforfreedom.org/search/censored_publications/result.html?author=&cauthor=&title=&country=7327&language=&censored_year=&censortype=&published_year=&censorreason=&sort=t&page=, i"))

不应该是(字符串最后十个字符的差异;引号移动)

webpage <- read_html(paste0(("http://search.beaconforfreedom.org/search/censored_publications/result.html?author=&cauthor=&title=&country=7327&language=&censored_year=&censortype=&published_year=&censorreason=&sort=t&page=", i))

paste0 在 R 中所做的是将两个字符串拼接在一起,没有任何分隔符。但你只有一根弦。所以它试图获取page=, i 的结果。但是您希望它通过page=40 获取page=1。因此,将 page=", i 之类的引号放在一起,以便将 URL 和 i 粘贴在一起。

我不是一个 R 程序员,但这简直让我大吃一惊。

Source 用于 paste0 行为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多