网络抓取在R中空的返回

发布于 2025-02-12 04:03:57 字数 503 浏览 2 评论 0原文

我试图从

library(rvest)

url <- "https://www.bloomberg.com/quote/WORLD:IND"

price <- read_html(url) %>% 
  html_nodes("div.overviewRow__66339412a5 span.priceText__06f600fa3e") %>% 
  html_text()

prevprice <- read_html(url) %>% 
  html_nodes("div.value__7e29a7c90d") %>% 
  html_text() #returns 0

prevprice <- read_html(url) %>% 
  html_nodes(xpath = '//section') %>%
  html_text() %>% 
  as.data.frame() #didn't find the price

提前致谢。

I'm trying to scrape prices from Bloomberg. I can get the current price as shown below but can't get the previous price. What's the wrong?

library(rvest)

url <- "https://www.bloomberg.com/quote/WORLD:IND"

price <- read_html(url) %>% 
  html_nodes("div.overviewRow__66339412a5 span.priceText__06f600fa3e") %>% 
  html_text()

prevprice <- read_html(url) %>% 
  html_nodes("div.value__7e29a7c90d") %>% 
  html_text() #returns 0

prevprice <- read_html(url) %>% 
  html_nodes(xpath = '//section') %>%
  html_text() %>% 
  as.data.frame() #didn't find the price

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聆听风音 2025-02-19 04:03:57

因此,至少有两个初始选项:

  1. 从脚本标签中提取该信息的从脚本标签中提取。当浏览器运行JavaScript时,此信息用于填充页面。使用rvest/httr,JavaScript不运行,因此您需要从脚本标签中提取,而不是在渲染的网页上最终出现。
  2. 或者,您可以使用百分比变更和当前价格来计算以前的价格。通过舍入可能存在很小的不准确性。

我在下面的代码中显示了以上两个选项。

我还将CSS选择器列表调整为使用attribute = value CSS选择器,并从operator(^)开始。这是为了使代码更加健壮,因为HTML中的类似乎是动态的,只有类属性值的开始是稳定的。


library(httr2)
library(tidyverse)
library(rvest)

url <- "https://www.bloomberg.com/quote/WORLDT:IND"
headers <- c("user-agent" = "mozilla/5.0")

page <- request(url) |>
  (\(x) req_headers(x, !!!headers))() |>
  req_perform() |>
  resp_body_html()

# extract direct
prev_price <- page |>
  html_text() |>
  stringr::str_match("previousClosingPriceOneTradingDayAgo%22%3A(\\d+\\.?\\d+?)%2C") |>
  .[, 2]
curr_price <- page |>
  html_element("[class^=priceText]") |>
  html_text() |>
  str_replace_all(",", "") |>
  as.numeric()

# calculate
change <- page |>
  html_element("[class^=changePercent]") |>
  html_text() |>
  str_extract("[\\d\\.]+") |>
  as.numeric()
adjustment <- 100 - change
prev_price_calculated <- curr_price * (adjustment / 100)

print(curr_price)
print(change)
print(prev_price)
print(prev_price_calculated)

So, there are at least two initial options:

  1. Extract from the script tag where that info is pulled from. When browser runs JavaScript this info is used to populate the page as you see it. With rvest/httr, JavaScript is not run, so you would need to extract from the script tag, rather than where it ends up on the rendered webpage.
  2. Or, you can calculate the previous price using the percentage change and current price. There might be a very small margin of inaccuracy here through rounding.

I show both of the above options in the code below.

I've also adapted the css selector list to use attribute = value css selectors, with starts with operator (^). This is to make the code more robust as the classes in the html appear to be dynamic, with only the start of the class attribute value being stable.


library(httr2)
library(tidyverse)
library(rvest)

url <- "https://www.bloomberg.com/quote/WORLDT:IND"
headers <- c("user-agent" = "mozilla/5.0")

page <- request(url) |>
  (\(x) req_headers(x, !!!headers))() |>
  req_perform() |>
  resp_body_html()

# extract direct
prev_price <- page |>
  html_text() |>
  stringr::str_match("previousClosingPriceOneTradingDayAgo%22%3A(\\d+\\.?\\d+?)%2C") |>
  .[, 2]
curr_price <- page |>
  html_element("[class^=priceText]") |>
  html_text() |>
  str_replace_all(",", "") |>
  as.numeric()

# calculate
change <- page |>
  html_element("[class^=changePercent]") |>
  html_text() |>
  str_extract("[\\d\\.]+") |>
  as.numeric()
adjustment <- 100 - change
prev_price_calculated <- curr_price * (adjustment / 100)

print(curr_price)
print(change)
print(prev_price)
print(prev_price_calculated)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文