You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用R自动提取奥地利就业数据动态网页的指定数据?

用R自动化爬取奥地利就业数据网站的方案

这个网站用了ASP.NET的UpdatePanel做AJAX动态加载,直接用rvest抓静态页面拿不到生成的数据,建议用RSelenium模拟浏览器操作,具体步骤如下:

1. 安装并加载必要工具包

install.packages(c("RSelenium", "rvest", "jsonlite"))
library(RSelenium)
library(rvest)
library(jsonlite)

2. 启动浏览器并打开目标页面

# 启动Chrome驱动(需确保Chrome浏览器和对应版本的chromedriver已安装)
driver <- rsDriver(browser = "chrome", port = 4567L)
remDr <- driver[["client"]]

# 打开目标数据网站
remDr$navigate("https://www.dnet.at/amis/Datenbank/DB_Be.aspx")

3. 模拟页面选项操作

  • 勾选左侧分类的Unselbständig Beschäftigte(对应id = id1.1的复选框)
target_checkbox <- remDr$findElement(using = "id", value = "id1.1")
target_checkbox$clickElement()
  • 第二个选择框保持默认,不勾选任何选项

  • 设置输出格式为时间序列(Zeitreihe,对应value = "ZR"的选项)

# 定位并点击对应选项
zr_option <- remDr$findElement(using = "css selector", value = "option[value='ZR']")
zr_option$clickElement()

# 如果是下拉框需要先展开再选择,替换成以下代码:
# output_dropdown <- remDr$findElement(using = "id", value = "下拉框实际ID")
# output_dropdown$clickElement()
# zr_option$clickElement()
  • 指定起始日期(需先通过浏览器F12工具确认日期输入框的真实ID)
start_date_input <- remDr$findElement(using = "id", value = "日期输入框实际ID")
# 清空原有内容,输入符合网站格式的日期(示例为DD.MM.YYYY格式)
start_date_input$clearElement()
start_date_input$sendKeysToElement(list("01.01.2010"))

4. 执行查询并等待数据加载

# 定位执行按钮(Ausführen),通过按钮文本定位
execute_btn <- remDr$findElement(using = "xpath", value = "//input[@value='Ausführen']")
execute_btn$clickElement()

# 根据网络速度调整等待时间,确保数据加载完成
Sys.sleep(5)

5. 提取目标数据

方式1:直接从页面面板(id = main_UpdatePanel1)提取表格

# 获取当前页面源码
page_source <- remDr$getPageSource()[[1]]
page_html <- read_html(page_source)

# 抓取面板内的表格数据
data_table <- page_html %>% 
  html_element("#main_UpdatePanel1 table") %>% 
  html_table(header = TRUE)

# 查看提取结果
print(data_table)

方式2:模拟导出JSON/XLSX后读取文件

# 预先设置Chrome下载路径,避免弹出保存对话框
chrome_options <- list(chromeOptions = list(prefs = list("download.default_directory" = "~/Downloads")))
driver <- rsDriver(browser = "chrome", port = 4567L, extraCapabilities = chrome_options)
remDr <- driver[["client"]]

# 定位并点击JSON导出按钮(需确认按钮真实ID)
export_json_btn <- remDr$findElement(using = "id", value = "JSON导出按钮实际ID")
export_json_btn$clickElement()

# 等待下载完成后读取JSON文件
json_data <- fromJSON("~/Downloads/下载的文件名.json")
print(json_data)

6. 关闭浏览器驱动

remDr$close()
driver$server$stop()

注意事项

  • Chrome浏览器与chromedriver版本必须匹配,否则无法正常启动
  • 页面元素的ID可能随网站更新变化,操作前需用浏览器开发者工具(F12)确认最新定位
  • 等待时间需根据自身网络速度调整,避免因数据未加载完成导致提取失败

内容的提问来源于stack exchange,提问作者Cettt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 07:20:22