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

R语言爬取谷歌股票价格失败:返回integer(0)求助

Hey there! Let's troubleshoot why you're hitting that frustrating integer(0) when trying to scrape Google's stock price. Here are the most likely issues and actionable fixes:

1. Why Your Current Approach Isn't Working
  • Dynamic Content & Anti-Scraping: Google Search pages rely heavily on dynamic loading and anti-bot measures. When you use read_html() without proper headers, you're probably getting a stripped-down version of the page that doesn't include the price nodes you're targeting.
  • Outdated Selectors: Google frequently updates its HTML class names (like ._FOc or .fac-l). The selectors you grabbed with SelectorGadget were likely temporary and have already changed.
Fixes to Try

Option 1: Scrape Google Finance Directly (More Stable)

Instead of scraping search results, target Google Finance's dedicated stock page—its structure is far more consistent for data extraction.

library(rvest)

# Direct URL for Google's stock (GOOGL on NASDAQ)
finance_url <- "https://www.google.com/finance/quote/GOOGL:NASDAQ"

# Simulate a browser request to avoid being blocked
page <- read_html(
  finance_url,
  user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"
)

# Extract the current price (re-check with SelectorGadget if this selector breaks later)
current_price <- page %>%
  html_node(".YMlKec.fxKbKc") %>%
  html_text()

cat("Google Stock Price:", current_price, "\n")

Option 2: Fix Headers for Google Search Results

If you still want to use the search page, you need to send a request that mimics a real browser to get the full, populated HTML.

library(rvest)
library(httr)

search_url <- "https://www.google.com/search?q=google+stock+price"

# Send a request with browser-like headers
response <- GET(
  search_url,
  add_headers(
    "User-Agent" = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36",
    "Accept-Language" = "en-US,en;q=0.9"
  )
)

# Parse the full response
page <- content(response, as = "parsed")

# Re-inspect the page source to find the latest selector (classes change often!)
# Example selector as of October 2023:
price <- page %>%
  html_node(".IsqQVc.NprOob.XcVN5d") %>%
  html_text()

print(price)
Pro Tips
  • Always right-click the page and select View Page Source after sending your request to confirm the target nodes exist in the static HTML. SelectorGadget sometimes picks up dynamically loaded elements that aren't present in the raw response.
  • Add delays with Sys.sleep(2) between requests if you're scraping multiple pages—Google will block you if you send too many requests too quickly.
  • For long-term projects, consider using a dedicated stock API (like Alpha Vantage or Yahoo Finance's yfinance package for R). These are designed for reliable data access and avoid scraping headaches entirely.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:40:12