Web scraping Rvest不会返回数据

Web scraping Rvest不会返回数据,web-scraping,rvest,httr,jsonlite,Web Scraping,Rvest,Httr,Jsonlite,我一直在尝试整理下表:您的问题是您的请求为您提供了一个html站点,而不是json响应。因此,将其解析为json失败,出现了您看到的错误。 (我不能确切地告诉您是因为您错过了accept_json()还是因为您使用的URL有点不正确。) 无论哪种方式,在链接的表后面反向工程API请求的要点,您必须将以下内容组合在一起: require(httr) require(dplyr) library(purrr) first_req <- GET("https://www.barcha

我一直在尝试整理下表:

您的问题是您的请求为您提供了一个html站点,而不是json响应。因此,将其解析为json失败,出现了您看到的错误。
(我不能确切地告诉您是因为您错过了
accept_json()
还是因为您使用的URL有点不正确。)

无论哪种方式,在链接的表后面反向工程API请求的要点,您必须将以下内容组合在一起:

require(httr)
require(dplyr)
library(purrr)

first_req <- GET("https://www.barchart.com")
xsrf_token <- cookies(first_req) %>% filter(name == 'XSRF-TOKEN') %>% pull(value) %>% URLdecode()

req <- GET(
    "https://www.barchart.com/proxies/core-api/v1/quotes/get",
    query = list(
      lists = "stocks.optionable.by_sector.all.us",
      fields = "symbol,symbolName,lastPrice,priceChange,percentChange,highPrice,lowPrice,volume,tradeTime,symbolCode,symbolType,hasOptions",
      orderBy = "symbol",
      orderDir = "asc",
      meta = "field.shortName,field.type,field.description",
      hasOptions = TRUE,
      #page = 1,
      #limit = 100,
      raw = 1
    ),
    content_type_json(),
    accept_json(),
    add_headers(
      "x-xsrf-token" = xsrf_token,
      "referrer" = "https://www.barchart.com/options/stocks-by-sector?page=1"
    )
  )

table_data <- req %>%
  content() %>%
  .$data %>%
  map_dfr(unlist)
require(httr)
需要(dplyr)
图书馆(purrr)
第一个请求%pull(值)%%>%URLdecode()
需求%
.$data%>%
地图(未列出)
这将为您提供4258项的完整列表,并将其强制转换为一个tible,以方便使用:)