使用 R 和 rvest 进行网页抓取 [英] Web scraping with R and rvest

查看:52
本文介绍了使用 R 和 rvest 进行网页抓取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 rvest 来学习使用 R 进行网络抓取.我正在尝试为页面的其他几个部分复制 Lego 示例并使用 selector gadget识别.

I am experimenting with rvest to learn web scraping with R. I am trying to replicate the Lego example for a couple of other sections of the page and using selector gadget to id.

我从 R Studio 中提取了示例教程.使用下面的代码,1 和 2 有效,但 3 无效.

I pulled the example from R Studio tutorial. With the code below, 1 and 2 work, but 3 does not.

library(rvest)
lego_movie <- html("http://www.imdb.com/title/tt1490017/")

# 1 - Get rating
lego_movie %>% 
  html_node("strong span") %>%
  html_text() %>%
  as.numeric()

# 2 - Grab actor names
lego_movie %>%
  html_nodes("#titleCast .itemprop span") %>%
  html_text()

# 3 - Get Meta Score 
lego_movie %>% 
  html_node(".star-box-details a:nth-child(4)") %>%
  html_text() %>%
  as.numeric()

推荐答案

我并没有真正掌握所有管道和相关代码的速度,所以可能有一些新的乱七八糟的工具可以做到这一点......但考虑到上面的答案让你"83/100",你可以这样做:

I'm not really up to speed on all of the pipes and associated code, so there's probably some new fandangled tools to do this...but given that the answer above gets you to "83/100", you can do something like this:

as.numeric(unlist(strsplit("83/100", "/")))[1]
[1] 83

我想用管道看起来会像这样:

Which I guess would look something like this with the pipes:

lego_movie %>% 
  html_node(".star-box-details a:nth-child(4)") %>%
  html_text(trim=TRUE) %>%
  strsplit(., "/") %>%
  unlist(.) %>%
  as.numeric(.) %>% 
  head(., 1)

[1] 83

或者按照 Frank 的建议,您可以使用以下内容评估表达式 "83/100":

Or as Frank suggested, you could evaluate the expression "83/100" with something like:

lego_movie %>% 
  html_node(".star-box-details a:nth-child(4)") %>%
  html_text(trim=TRUE) %>%
  parse(text = .) %>%
  eval(.)
[1] 0.83

这篇关于使用 R 和 rvest 进行网页抓取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆