【问题标题】:How to scrape data within a input tag from an iframe using R如何使用 R 从 iframe 中抓取输入标签中的数据
【发布时间】:2019-01-28 06:41:40
【问题描述】:

我正在尝试从一个学术项目的属性门户网站中抓取数据。我感兴趣的数据是价格趋势,它在 iframe 中。我想获取上限、平均和下限的数据。该数据存储在输入标签中。我试图通过引用父类然后输入标签来抓取这些数据,但无法获取数据。

我需要抓取许多 iframe,但其中一个是 this

我尝试过的代码如下,但我没有得到想要的结果。

#Specifying the url of the iframe to be scraped
url <- 'https://www.99acres.com/do/pricetrends?building_id=0&loc_id=12400&prop_type=1&pref=S&bed_no=0&w=600&h=350'

#Reading the HTML code from the website
download.file(url, destfile = "scrapedpage.html", quiet=TRUE)
webpage <- read_html("scrapedpage.html")

PriceTrend_data_html <- html_nodes(webpage,'.ptplay input')

PriceTrend_data_html

如果有人可以在这里指导我,那将非常有帮助。

【问题讨论】:

  • 在任何地方都找不到输入类
  • 它不是一个类,而是一个输入标签,存储了我想要的所有数据。如果您搜索输入标签,它位于源页面底部,id="priceTrendVariables"。它有我需要的所有数据。
  • 网址无效(访问被拒绝)。
  • @Alexandregeorges 你的意思是它不会只为你打开,因为我仍然可以在我最后打开它吗?虽然 read_html() 在我使用 download.file() 下载文件时不会读取它,但是当我复制源代码并将其手动保存为 html 时它会很好地读取它。关于为什么会发生这种情况的任何想法?它与访问权有什么关系吗?

标签: html css r web-scraping rvest


【解决方案1】:

经过一些研究,我能够自己解决它,因此将其发布在这里,以防其他人将来遇到同样的问题。当我使用 download.file() 下载文件时,我无法使用 read_html() 读取 html 文件,因此必须手动下载文件然后处理它。

由于数据只在输入标签内,所以我用输入标签的 id 抓取属性并得到我想要的数据。这是对我有用的一段代码。

url <- read_html("scrapedpage_chart.html")
average_prices <- html_attr(html_nodes(url, "#priceTrendVariables"), "median")
average_prices <- gsub(pattern = 'null',replacement = 'NA',x = average_prices)
average_prices <- unlist(strsplit(average,split = ","))
average_prices <- as.numeric(average)
average_prices

【讨论】:

    猜你喜欢
    • 2019-12-01
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    相关资源
    最近更新 更多