【问题标题】:Scraping The Lego Movie on IMDb using rvest in R使用 R 中的 rvest 在 IMDb 上抓取乐高电影
【发布时间】:2016-03-14 06:27:18
【问题描述】:

我正在学习rvest 0.3.1 包。

使用 this tutorial 的代码,我只将 html 更改为 read_html

library(rvest)
# Store web url
lego_movie <- read_html("http://www.imdb.com/title/tt1490017/")
#Scrape the website for the movie rating
rating <- lego_movie %>%
  html_nodes("strong span") %>%
  html_text() %>%
  as.numeric()
rating 

# Scrape the website for the cast
cast <- lego_movie %>%
  html_nodes("#titleCast .itemprop span") %>%
  html_text()
cast

但是,评分和演员只是返回

numeric(0)
character(0)

如有任何解决此问题的建议,我们将不胜感激?

更新

我在R 3.2.3 上使用rvest v0.3.1xml2 v0.1.2,但仍然评分和演员回报

numeric(0)
character(0)

下面是逐步运行代码

rating <- lego_movie
rating
{xml_document}
<html>
[1] <head>\n    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>\n    <title>\n      Web Filter Block Override\n    </title>\n    <style type="text/css"><![CDAT ...
[2] <body class="authenticate">\n    <div class="header">\n      <h2>\n        Powered By Fortinet\n      </h2>\n      <h1>\n        FortiGuard Web Filtering\n      </h1>\n    </di ...
rating <- lego_movie %>% 
   html_nodes("strong span")
rating
{xml_nodeset (0)}
rating <- lego_movie %>% 
   html_nodes("strong span") %>%
   html_text()
rating
character(0)
rating <- lego_movie %>% 
   html_nodes("strong span") %>%
   html_text() %>%
   as.numeric()
rating
numeric(0)

【问题讨论】:

  • 你的代码对我有用
  • @Symbolix 感谢您花时间尝试。为什么你认为它不适合我?
  • 是否正确安装了xml2
  • @Symbolix 我将它安装为其他包 install.packages("xml2")。有具体的安装方式吗?
  • 我不知道,我只是猜测它可能是个问题。

标签: r rvest


【解决方案1】:

代码只需在开头添加以下行即可工作

Sys.setenv(http_proxy="http_proxy=tur-cache2.massey.ac.nz:8080 http_proxy_user=ask")

【讨论】:

    【解决方案2】:

    R 3.2.3 上使用rvest v0.3.1(和xml2 v0.1.2),您使用的代码应该可以工作。这是它在我的环境中工作的屏幕截图

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-14
      • 2022-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多