【问题标题】:Scrape data HTML table in iframe using R使用 R 在 iframe 中抓取数据 HTML 表
【发布时间】:2022-01-13 16:41:16
【问题描述】:

我正在尝试从此链接中抓取下表:

https://www.price.moc.go.th/en/home_en

使用rvest 以下内容不起作用,但我不知道要更改什么:

读取html代码

html <- read_html("https://www.price.moc.go.th/en/home_en")

进入特定的css选择器

check <- html %>%
  html_nodes("#cpi_index > td:nth-child(2)") %>%
  html_text()

enter image description here

【问题讨论】:

    标签: html r web-scraping rvest rselenium


    【解决方案1】:

    所需的表在iframe,其中有一个链接,

    https://www.price.moc.go.th/price_index/index_price01.html

    您需要RSelenium 才能获得该表。

    url = 'https://www.price.moc.go.th/price_index/index_price01.html'
    #start the browser
    library(RSelenium)
    library(rvest)
    library(dplyr)
    driver = rsDriver(browser = c("firefox"))
    remDr <- driver[["client"]]
    remDr$navigate(url)
    #get table
    df = remDr$getPageSource()[[1]] %>% 
      read_html() %>%
      html_table() 
    
    df[[1]]
    # A tibble: 4 x 5
      INDEX    `Nov  21` `M/M` `Y/Y` `A/A`
      <chr>        <dbl> <dbl> <dbl> <dbl>
    1 CPI           102.  0.28  2.71  1.15
    2 CORE-CPI      101.  0.09  0.29  0.23
    3 PPI           106.  1.2   8.5   4.4 
    4 CMI           116   0.5  10.4   7.9 
    

    【讨论】:

    • 亲爱的@Nad Pat,非常感谢!出于好奇,您如何发现表格的格式为 iframe ?
    • 修改了答案以包括代表iframe的图片和位于它旁边的链接。
    • 非常感谢@Nad Pat!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    • 2010-11-26
    • 2020-09-28
    相关资源
    最近更新 更多