【问题标题】:R: Missed values when scraping webpageR:抓取网页时丢失的值
【发布时间】:2017-11-06 14:34:08
【问题描述】:

从网页抓取数据时,某些元素/值不会返回。

具体来说,我是用rvest包报废的。

包含我想要的信息的网页是https://azure.microsoft.com/en-us/pricing/details/virtual-machines/windows/ - 但是,当我废弃数据时,带有价格的列只返回“$-”。

示例代码:

library(rvest)

webpage <- read_html("https://azure.microsoft.com/en-us/pricing/details/virtual-machines/windows/")
tbls <- html_nodes(webpage, "table")

tbls_ls <- webpage %>%
  html_nodes("table") %>%
  .[1:(length(tbls)-2)] %>%
  html_table()

第一个df的输出:

> List of 22  $ :'data.frame':  7 obs. of  6 variables:   ..$ Instance   
> : chr [1:7] "B1L" "B1S" "B2S" "B1MS" ...   ..$ Cores                  
> : int [1:7] 1 1 2 1 2 4 8   ..$ RAM                                   
> : chr [1:7] "0.50 GiB" "1.00 GiB" "4.00 GiB" "2.00 GiB" ...   ..$
> Temporary Storage                            : chr [1:7] "1 GiB" "2
> GiB" "8 GiB" "4 GiB" ...   ..$ Price                                  
> : chr [1:7] "$-" "$-" "$-" "$-" ...   ..$ Prices with Azure Hybrid
> Benefit1 (% savings): chr [1:7] "$-" "$-" "$-" "$-" ...

我可以做些什么来获得这些特定元素的全部价值?

【问题讨论】:

    标签: html r dataframe web-scraping missing-data


    【解决方案1】:

    无论过滤器如何,它们都有一组价格数据。因此,您需要获取该属性的值并解析 json。

    library(rvest)
    
    webpage <- read_html("https://azure.microsoft.com/en-us/pricing/details/virtual-machines/windows/")
    tbls <- html_nodes(webpage, "table")
    
    webpage %>%
      html_nodes("table") %>%
      .[1:(length(tbls)-2)] %>%
      html_table()
    
    
    ss <- webpage %>% html_nodes("table span.price-data ") %>% xml_attr('data-amount') 
    
    lapply(ss,function(x){data.frame(jsonlite::fromJSON(x))})
    

    样本输出:

    [[176]]
      regional.asia.pacific.southeast regional.australia.east regional.canada.central regional.canada.east
    1                           1.496                   1.496                   1.376                1.376
      regional.europe.west regional.japan.east regional.united.kingdom.south regional.us.east.2 regional.usgov.virginia
    1                1.488               1.464                         1.448              1.373                   1.504
      regional.us.west regional.us.west.2
    1            1.376              1.248
    
    [[177]]
      regional.asia.pacific.southeast regional.australia.east regional.canada.central regional.canada.east
    1                           4.464                   4.464                   4.224                4.224
      regional.europe.west regional.japan.east regional.united.kingdom.south regional.us.east.2 regional.usgov.virginia
    1                4.448                 4.4                         4.368              4.365                    4.48
      regional.us.west regional.us.west.2
    1            4.224              3.968
    

    您需要匹配该特定值并从中获取价格。

    【讨论】:

    • 谢谢你——它就像一个魅力。但是,你能分享一下直觉吗?我似乎无法检测到有一个 json 文件,你在哪里看到的?
    • 不,没有 json 文件,它只是 json 数据,如果您看到抓取的价格数据 - 您会看到带有大括号和 : 的嵌套数据,非常类似于键值对json结构。
    • 啊,现在我明白了。如果我想为每个地区的每个价格方案设置一列怎么办?也就是说,PriceScenarioA.RegionX、PriceScenarioA.RegionY、PriceScenarioB.RegionX、PriceScenarioB.RegionY 之类的?
    猜你喜欢
    • 2020-11-01
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 2017-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多