Ошибка в curl :: curl_fetch_memory (url, handle = handle): ошибка отправки: соединение было сброшено (RStudio.cloud) - PullRequest
1 голос
/ 07 ноября 2019

Я хочу получить id_product и id_parent с этой веб-страницы. Вчера я мог получить результаты, но когда я попробовал это сегодня, я получил сообщение об ошибке. Во всяком случае, я делаю это из rstudio.cloud .

url <-  paste("https://www.tokopedia.com/zhafranseafood/cumi-asin-1kg-per-pack")

    headers = c('User-Agent' = 'Mozilla/5.0')
    doc <- read_html(httr::GET(url, httr::add_headers(.headers=headers)))%>%
          html_text()
    id_product <- str_match_all(doc,'product_id\\s+=\\s+(\\d+);')[[1]][,2]
    id_parent <- str_match_all(doc,'parent_id\\s+=\\s+(\\d+);')[[1]][,2]

    id_product
    id_parent

Error in curl::curl_fetch_memory(url, handle = handle) : 
  Send failure: Connection was reset

Я пытался найти возможное объяснение, но все еще безрезультатно.

1 Ответ

1 голос
/ 07 ноября 2019

Для сервера требуется дополнительный заголовок

library(httr)
library(stringr)
library(magrittr)

headers = c(
  'User-Agent' = 'Mozilla/5.0',
  'Accept' = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3'
)

doc <- read_html(httr::GET(url = 'https://www.tokopedia.com/zhafranseafood/cumi-asin-1kg-per-pack', httr::add_headers(.headers=headers)))%>%
       html_text()

id_product <- str_match_all(doc,'product_id\\s+=\\s+(\\d+);')[[1]][,2]
id_parent <- str_match_all(doc,'parent_id\\s+=\\s+(\\d+);')[[1]][,2]

id_product
id_parent
...