gpt4 book ai didi

html - 如何使用 R 从 iframe 的输入标签中抓取数据

转载 作者:行者123 更新时间:2023-11-28 00:31:11 25 4
gpt4 key购买 nike

我正在尝试从一个学术元素的属性门户中抓取数据。我感兴趣的数据是价格趋势,它位于 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

如果有人能在这里指导我,那将是非常有帮助的。

最佳答案

经过一些研究,我能够自己解决它,因此将其张贴在这里,以防其他人将来遇到同样的问题。当我使用 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

关于html - 如何使用 R 从 iframe 的输入标签中抓取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54396749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com