【问题标题】:If condition using json to scrape multiple links如果条件使用 json 来抓取多个链接
【发布时间】:2019-08-23 10:04:51
【问题描述】:

我正在使用 json 来抓取多个 (1000) 链接的内容。但是,有些链接不能以 json 格式工作,因此没有要抓取的内容。因此,我的代码在找到其中一个链接时停止工作。

我尝试使用TryCatch 来避免错误,但它似乎不起作用

这是我正在使用的代码

library(jsonlite)
library(rvest)

lapply(links_jason[1:6], function(x) {
  tryCatch(
    {
  json_data <- read_html(x) %>% html_text()%>%
    jsonlite::fromJSON(.)%>%
    select(1)
    },
  error = function(cond) return(NULL),
  finally = print(x)
  )
})

这是我遇到的问题

Debug location is approximate beacuse the source is not available

以下是我尝试抓取的链接的一些示例

链接 1、2 和 6 工作正常。 3、4和5需要避免

> head(links_jason)
[1] "https://lasillavacia.com/silla_llena_api/get?path=/contenido-nodo/68077&_format=hal_json"
[2] "https://lasillavacia.com/silla_llena_api/get?path=/contenido-nodo/57833&_format=hal_json"
[3] "https://lasillavacia.com/silla_llena_api/get?path=/contenido-nodo/56774&_format=hal_json"
[4] "https://lasillavacia.com/silla_llena_api/get?path=/contenido-nodo/56748&_format=hal_json"
[5] "https://lasillavacia.com/silla_llena_api/get?path=/contenido-nodo/56782&_format=hal_json"
[6] "https://lasillavacia.com/silla_llena_api/get?path=/contenido-nodo/64341&_format=hal_json"

我也尝试过使用没有结果的 if 语句。有人可以帮忙吗?谢谢!

【问题讨论】:

标签: r json web-scraping rvest


【解决方案1】:

用jsonlite直接读取并测试返回长度

library(jsonlite)
library(rvest)
library(magrittr)

links_jason <- c("https://lasillavacia.com/silla_llena_api/get?path=/contenido-nodo/68077&_format=hal_json"
,"https://lasillavacia.com/silla_llena_api/get?path=/contenido-nodo/57833&_format=hal_json"
, "https://lasillavacia.com/silla_llena_api/get?path=/contenido-nodo/56774&_format=hal_json"
, "https://lasillavacia.com/silla_llena_api/get?path=/contenido-nodo/56748&_format=hal_json"
, "https://lasillavacia.com/silla_llena_api/get?path=/contenido-nodo/56782&_format=hal_json"
,"https://lasillavacia.com/silla_llena_api/get?path=/contenido-nodo/64341&_format=hal_json")


lapply(links_jason[1:6], function(x) {

      json_data <- jsonlite::read_json(x)
      if(length(json_data)>0){
        print(x)
      }
}

或者类似的东西:

library(jsonlite)
library(rvest)
library(magrittr)

links_jason <- c("https://lasillavacia.com/silla_llena_api/get?path=/contenido-nodo/68077&_format=hal_json"
,"https://lasillavacia.com/silla_llena_api/get?path=/contenido-nodo/57833&_format=hal_json"
, "https://lasillavacia.com/silla_llena_api/get?path=/contenido-nodo/56774&_format=hal_json"
, "https://lasillavacia.com/silla_llena_api/get?path=/contenido-nodo/56748&_format=hal_json"
, "https://lasillavacia.com/silla_llena_api/get?path=/contenido-nodo/56782&_format=hal_json"
,"https://lasillavacia.com/silla_llena_api/get?path=/contenido-nodo/64341&_format=hal_json")


lapply(links_jason[1:6], function(x) {
      json_data <- jsonlite::read_json(x)
      if(length(json_data)==0){
        json_data <- NA}
      else{
          print('doing something with json_data')
        }
      })

【讨论】:

  • 是否可以使用if 包括这部分代码json_data &lt;- read_html(x) %&gt;% html_text()%&gt;% 我尝试使用lapply(links_jason[1:6], function(x) { json_data &lt;- read_html(x) %&gt;% html_text()%&gt;% if(length(json_data)&gt;0) json_data &lt;- NA 但我得到argument is not interpretable as logical
  • 查看 edt 以了解解决方法
  • 这是我使用第二个选项&gt; json_data [1] NA 得到的结果,我没有得到每个链接的内容,没有你建议的选项。这是为什么呢?
  • 因为我还没有写任何东西来提取内容。如果 url 很好,我只是保留为 json_data - 然后你可以提取你想要的任何东西,或者如果 url 不好,我将 N/A 分配给 json_data。您可以添加一个 else,然后在那里进行提取。
  • 谢谢!我分配了 NA,然后我对内容进行了抓取。
猜你喜欢
  • 2022-01-08
  • 2017-12-24
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
  • 2021-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多