【问题标题】:R rvest html scrapingR rvest html 抓取
【发布时间】:2015-03-30 16:31:16
【问题描述】:

我有一个类似这样的 R 脚本:

id <- "25731"
url_name <- "Cross_Ange:_Tenshi_to_Ryuu_no_Rondo"
library(rvest)
html_content <- html(paste("http://myanimelist.net/anime/", id, "/", url_name, "/stats", sep=""))
test_page <- html_node(html_content, "div")

test_page 变量仅用于检查页面是否正确加载。问题是有时它不会。有时html_content 变量包含一些奇怪的 HTML 内容,比如

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html style="height:100%">
<head>
<meta name="ROBOTS" content="NOINDEX, NOFOLLOW">
<meta name="format-detection" content="telephone=no">
<meta name="viewport" content="initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
</head>
<body style="margin:0px;height:100%"><iframe src="/_Incapsula_Resource?CWUDNSAI=9&amp;xinfo=6-12427765-0%200NNN%20RT(1427731440619%201)%20q(0%20-1%20-1%20-1)%20r(0%20-1)%20B12(4,315,0)&amp;incident_id=124002150019133827-71376390758075766&amp;edet=12&amp;cinfo=04000000" frameborder="0" width="100%" height="100%" marginheight="0px" marginwidth="0px">Request unsuccessful. Incapsula incident ID: 124002150019133827-71376390758075766</iframe></body>
</html>

那是错误的 HTML 内容。甚至在错误的内容中也不一致。有时它会返回另一个错误页面。

URL 本身是正确的,因为如果我尝试使用 Firefox 发送 HTTP 请求,它会在第一次尝试时按预期返回正确的 HTML。

奇怪的是,如果我尝试使用 html() 函数多次运行该行,最终它会加载正确的 HTML 页面而无需我进行任何更改。奇怪的不一致,当我尝试使用 RScript 自动执行时,这很糟糕。

我设置了一个 while 循环来检查 HTML 是否已正确加载(如果它使用 html_node() 找到任何 div 标记),但 RScript 会抛出错误,而 RStudio 执行得很好:

Error in as.vector(x, "list") :
  cannot coerce type 'environment' to vector of type 'list'
Calls: html_node ... <Anonymous> -> lapply -> as.list -> as.list.default
Execution halted

总之,RStudio 有一个不一致的html() 函数,它有时会返回一个奇怪的页面,但如果我可以通过反复尝试执行该行来强制通过它,它最终会起作用。但是 RScript 直接抛出错误。

R 版本 3.1.3 (2015-03-09) -- “Smooth Sidewalk”

【问题讨论】:

标签: html r web-scraping rvest


【解决方案1】:

您尝试加载的网站使用名为 Incapsula (www.incapsula.com) 的服务。它可以防止机器人访问其内容。

【讨论】:

    【解决方案2】:

    这对我来说是第一次尝试吗?

    version  R version 3.3.1 (2016-06-21)
    system   x86_64, linux-gnu           
    ui       RStudio (0.99.903)          
    language (EN)                        
    collate  en_US.UTF-8                 
    tz       <NA>                        
    date     2016-09-23 
    
    
    u <-'http://myanimelist.net/anime/25731/Cross_Ange:_Tenshi_to_Ryuu_no_Rondo/stats'
    
    a <- html(u)
    
    b <- (html_nodes(a,"#content table")) %>% html_table(fill = T)
    
    colnames(b[[4]]) <- b[[4]][1,] %>% unlist %>% as.character
    
    b[[4]] <- b[[4]][2:nrow(b[[4]]),]
    
    > head(b[[4]])
    ===  ===============  =====  =============  ========  ==============
    \    Member           Score  Status         Eps Seen  Activity      
    ===  ===============  =====  =============  ========  ==============
    2    Kirito_Kun36     7      Completed      25 / 25   11 minutes ago
    3    StargazerM       -      On-Hold        - / 25    14 minutes ago
    4    Frosti_Limbu     -      Plan to Watch            21 minutes ago
    5    ShadowGekko      -      Plan to Watch            37 minutes ago
    6    meedly           -      Watching       12 / 25   39 minutes ago
    7    cowboyninjabear  5      Completed      25 / 25   55 minutes ago
    ===  ===============  =====  =============  ========  ==============
    

    【讨论】:

      猜你喜欢
      • 2020-04-19
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 2018-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多