找不到下载的URL(使用R进行网页抓取)

问题描述 投票:0回答:1

我只想用R抓取网页并自动下载文件。

https://www.maklarstatistik.se/omrade/riket/stockholms-lan/stockholm/#/bostadsratter/arshistorik-prisutveckling

如果你打开这个网页,你可以找到这样的按钮

enter image description here

我检查了源代码,检查了与下载按钮对应的元素 并尝试找出 xlsx 文件名

但我找不到任何东西。

我只找到了这个,但它不是文件网址。 enter image description here

我只是想知道如何找到下载文件的 URL。 或者如何模拟生成 xlsx 文件,就像我使用 R 单击按钮一样

非常感谢!

r web-scraping download
1个回答
0
投票

我已经能够使用以下代码下载该文件:

library(RDCOMClient)
url <- "https://www.maklarstatistik.se/omrade/riket/stockholms-lan/stockholm/#/bostadsratter/arshistorik-prisutveckling"
IEApp <- COMCreate("InternetExplorer.Application")
wsh <- COMCreate("Wscript.Shell")

IEApp[['Visible']] <- TRUE
IEApp$Navigate(url)
Sys.sleep(5)
doc <- IEApp$document()
web_Obj_Accept <- doc$getElementByID("cc-b-acceptall")

clickEvent <- doc$createEvent("MouseEvent")
clickEvent$initEvent("click", TRUE, FALSE)
tryCatch(web_Obj_Accept$dispatchEvent(clickEvent), error = function(e) NA)

Sys.sleep(5)

web_Obj_Download <- doc$querySelector("#main > div > div.housing-tenure.column.small-12.active > section:nth-child(1) > div > div.tabgroup-view.table-view.active > div.actions-container.button-group > button")
web_Obj_Download$dispatchEvent(clickEvent)

wsh$AppActivate("Explorer")
wsh$SendKeys("%{s}", TRUE)
Sys.sleep(5)
wsh$SendKeys("{Enter}", TRUE)
wsh$AppActivate("Explorer")
wsh$SendKeys("%{s}", TRUE)
wsh$SendKeys("{Enter}", TRUE)
© www.soinside.com 2019 - 2024. All rights reserved.