【问题标题】:Error in open.connection(x, "rb") : HTTP error 404 in Ropen.connection(x, "rb") 中的错误:R 中的 HTTP 错误 404
【发布时间】:2020-02-15 05:55:07
【问题描述】:

我正在尝试使用 R 抓取数据以获取有关以下网站中某些列表的详细信息,但我收到一个错误,我不确定如何解决:open.connection(x, "rb") 中的错误: HTTP 错误 404 我尝试使用 httr 包并尝试在类似帖子中看到的功能,但无法解决它。我是不是做错了什么?

library(XML)
library(RCurl)
library(curl) 
library(rvest)
library(tidyverse)
library(dplyr)
library(httr) 

url <- "https://www.sgcarmart.com/new_cars/index.php"
cardetails <- read_html(url)

listing <- html_nodes(cardetails, "#nc_popular_car")
popularcars <- html_nodes(listing,".link")
count<-length(popularcars)


info <- data.frame(CarName=NA, Distributer=NA, Hotline= NA, CountryBuilt= NA, Predecessor= NA, stringsAsFactors = F )

for(i in 1:count)
{
  h <- popularcars[[i]]

details_url <- paste0("https://www.sgcarmart.com/new_cars",html_attr(h,"href"))

details <- read_html(details_url)

info[i,]$CarName <- html_node(details,".link_redbanner")

}

info

【问题讨论】:

    标签: r rvest


    【解决方案1】:

    TL;DR

    添加斜线:

      details_url <- paste0("https://www.sgcarmart.com/new_cars/",html_attr(h,"href"))
      # --->          --->          --->          --->         ^
    

    旅程

    1. 我将您的源代码运行得足够远以获取popularcars,并查看了第一个:

      h <- popularcars[[1]]
      h
      # {html_node}
      # <a href="newcars_overview.php?CarCode=12618" class="link">
      # [1] <div style="position:relative; padding-bottom:6px;">\r\n                                < ...
      # [2] <div style="padding-bottom:3px;" class="limittwolines">Toyota Corolla Altis</div>
      details_url <- paste0("https://www.sgcarmart.com/new_cars",html_attr(h,"href"))
      details_url
      # [1] "https://www.sgcarmart.com/new_carsnewcars_overview.php?CarCode=12618"
      

      像你一样,对我来说那个 URL 返回 404。

    2. 我(在一个无聊的普通浏览器中)导航到主 URL,查看页面的来源,然后搜索 12618

      <div style="padding:10px 10px 5px 10px;" id="nc_popular_car">
                                              <div class="floatleft" style="text-align:center;width:136px;padding-right:22px;">
                              <a href="newcars_overview.php?CarCode=12618" class="link">
                                  <div style="position:relative; padding-bottom:6px;">
                                      <div style="position:absolute; border:1px solid #B9B9B9; width:134px; height:88px;"><img src="https://i.i-sgcm.com/images/spacer.gif" width="1" height="1" alt="spacer" /></div>
                                      <img src="https://i.i-sgcm.com/new_cars/cars/12618/12618_m.jpg" width="136" height="90" border="0" alt="Toyota Corolla Altis" />
                                  </div>
                                  <div style="padding-bottom:3px;" class="limittwolines">Toyota Corolla Altis</div>
                              </a>
                              <div style="padding-bottom:14px;" class="font_black">$91,888</div>
                          </div>
      
    3. 我右击&lt;a href="newcars_overview.php?CarCode=12618" class="link"&gt;部分,复制了“链接位置。我发现是:

      https://www.sgcarmart.com/new_cars/newcars_overview.php?CarCode=12618 <-- from the source
      https://www.sgcarmart.com/new_carsnewcars_overview.php?CarCode=12618  <-- from your code
      

    顺便说一句,您可能会发现这比for 循环更容易管理。迭代地构建一个框架是非常低效的,虽然对于我发现的 18 个条目来说它可能还不错,但从长远来看它并不好(如果你可以避免的话)。

    info <- lapply(popularcars, function(h) {
        details_url <- paste0("https://www.sgcarmart.com/new_cars/", html_attr(h,"href"))
        details <- read_html(details_url)
        html_text(html_node(details,".link_redbanner"))
      })
    
    str(info)
    # List of 18
    #  $ : chr "Toyota Corolla Altis"
    #  $ : chr "Hyundai Venue"
    #  $ : chr "Hyundai Avante"
    #  $ : chr "SKODA Octavia"
    #  $ : chr "Honda Civic"
    #  $ : chr "Mazda 3 Sedan Mild Hybrid"
    #  $ : chr "Honda Jazz"
    #  $ : chr "Kia Cerato"
    #  $ : chr "Mazda CX-5"
    #  $ : chr "Mercedes-Benz GLA-Class(Parallel Imported)"
    #  $ : chr "Toyota Raize(Parallel Imported)"
    #  $ : chr "Toyota Camry Hybrid(Parallel Imported)"
    #  $ : chr "Mercedes-Benz A-Class Hatchback(Parallel Imported)"
    #  $ : chr "Mercedes-Benz A-Class Saloon(Parallel Imported)"
    #  $ : chr "Honda Fit(Parallel Imported)"
    #  $ : chr "Mercedes-Benz C-Class Saloon(Parallel Imported)"
    #  $ : chr "Mercedes-Benz CLA-Class(Parallel Imported)"
    #  $ : chr "Honda Freed Hybrid(Parallel Imported)"
    

    最后一点:虽然这是一项值得学习的努力,但该网站的Terms of Service 明确声明:“您同意您不会:...从事大规模自动化,系统或以任何形式提取我们网站上的材料(“内容”)”。我认为你的努力是在这个限制之下。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-18
      • 2021-06-19
      • 1970-01-01
      • 2020-09-22
      • 1970-01-01
      • 2021-10-31
      • 2018-05-27
      相关资源
      最近更新 更多