【问题标题】:Error in curl::curl_fetch_memory(url, handle = handle) : Send failure: Connection was reset (RStudio.cloud)curl :: curl_fetch_memory(url,handle = handle)中的错误:发送失败:连接已重置(RStudio.cloud)
【发布时间】:2020-03-03 14:27:35
【问题描述】:

我想从此网页获取 id_productid_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

我一直在寻找可能的解释,但仍然无济于事。

【问题讨论】:

    标签: r web-scraping rcurl httr


    【解决方案1】:

    服务器需要额外的标头

    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
    

    【讨论】:

    • 我还是做不到。它像永远一样加载。无论如何,我是从 rstudio.cloud 做的。
    • 我不确定这有什么不同。我很好地从 R Studio 运行了上述内容。在浏览器https://www.tokopedia.com/zhafranseafood/cumi-asin-1kg-per-pack 中打开此网址并检查其网络流量。查看初始请求中使用的标头。首先重新创建整个集合,看看会发生什么。在您的情况下,可能会发生其他事情,但对我来说,服务器需要第二个标头。
    猜你喜欢
    • 2018-12-26
    • 2016-11-11
    • 2018-12-18
    • 2020-03-31
    • 2017-12-14
    • 2022-01-23
    • 2021-11-26
    • 1970-01-01
    • 2015-10-22
    相关资源
    最近更新 更多