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

用R抓取动态网页Google可视化估值表无结果,请求排查问题

Why Your Rvest Code Isn't Fetching the Valuation Table

Let’s break down why your code isn’t returning results, plus actionable fixes to grab that market valuation table:

Core Issues

  • Dynamic JavaScript Rendering: The google-visualization-table-table table doesn’t exist in the initial static HTML that rvest pulls. This page uses JavaScript to generate and inject the table into the page after load—and rvest doesn’t execute JavaScript, so it can’t see elements created this way.
  • Location Permission Prompt: The site’s location access request may alter content loading flow, but even if you decline, the root problem remains the JS-dependent table rendering.

Fix 1: Use RSelenium to Simulate a Real Browser

RSelenium controls a browser (like Chrome) that executes JavaScript, so it can capture the fully rendered page. Here’s a working implementation:

library(RSelenium)
library(rvest)

# Initialize Chrome driver (ensure Chrome + chromedriver are installed and in your PATH)
driver <- rsDriver(browser = "chrome", port = 4567L)
remDr <- driver[["client"]]

# Navigate to the target page
remDr$navigate("https://www.starcapital.de/en/research/stock-market-valuation/")

# Wait 3 seconds for the table to load (adjust timing if needed)
Sys.sleep(3)

# Extract rendered page source and parse with rvest
page_source <- remDr$getPageSource()[[1]]
valuation <- read_html(page_source) %>% 
  html_nodes(xpath='//*[@id="infotable_div2"]/div/div/table') %>% 
  html_table()

valuation <- valuation[[1]]

# Clean up: shut down the driver
remDr$close()
driver$server$stop()

Fix 2: Extract Data via Inline JavaScript (Browser DevTools Required)

If you prefer not to use a browser simulator, you can dig into the page’s source code via your browser’s DevTools to find raw data stored in JavaScript variables. Use the V8 package to execute that JS and convert the data to an R data frame. This requires identifying the specific variable holding the table data (look for arrays or objects with valuation metrics).

Pro Tip: Fetch Directly from the Site’s API

Check your browser’s Network tab (DevTools) for API calls that load the table data as JSON. Most dynamic sites pull data via backend APIs, which you can call directly with httr or jsonlite—this is usually faster than simulating a browser.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 10:57:34