【问题标题】:Web-Scraping with rvest doesn't work使用 rvest 进行网络抓取不起作用
【发布时间】:2017-11-03 17:39:45
【问题描述】:

我正在尝试从这个网站上抓取 cmets:

http://www.latercera.com/noticia/trabajos-realizan-donde-viven-los-extranjeros-tienen-residencia-chile/

这是我执行此任务的代码。

url <- 'http://www.latercera.com/noticia/trabajos-realizan-donde-viven-los-extranjeros-tienen-residencia-chile/'

webpage <- read_html(url)

data_html <- html_nodes(webpage,"gig-comment-body")

不幸的是,rvest 似乎无法通过 CSS 选择器 (gig-comment-body) 识别节点。

nodes 是一个空列表,所以它不会抓取任何东西。

【问题讨论】:

  • 我从页面源代码中猜测是 cmets 是通过 javascript 加载的。您需要使用 RSelenium 之类的东西(也许还有“PhantomJS”无头浏览器实例)。

标签: r rvest


【解决方案1】:

这是另一个没有 docker 的 rselenium 解决方案

install.packages("RSelenium")

library (RSelenium)


driver<- rsDriver()
remDr <- driver[["client"]]

remDr$navigate("http://www.latercera.com/noticia/trabajos-realizan-donde-viven-los-extranjeros-tienen-residencia-chile/")

elem <- remDr$findElement( using = "id",value = "commentsDiv-779453")
#or
elem <- remDr$findElement( using = "class name", "gig-comments-comments")

elem$highlightElement() # just for interactive use in browser.

elemtxt <- elem$getElementAttribute("outerHTML") # gets us the HTML

【讨论】:

  • "without docker" 意味着用户可以在没有 docker 的情况下运行 Selenium,这绝对不是给定的,尤其是对于 Windows 系统上的用户,但在 macOS 上也越来越多。事实上,很大一部分人最终在 Docker b/c 中使用它,然后各种 *Drivers “正常工作”。 “没有 docker”是可行的,但绝对不是“给定的”。
【解决方案2】:

@r2evans 是正确的。它使用 javascript 构建评论 &lt;div&gt;s,它还需要延迟。我更喜欢 Splash 而不是 Selenium(虽然我制作了 splashr,所以我并不完全公正):

library(rvest)
library(splashr)

URL <- 'http://www.latercera.com/noticia/trabajos-realizan-donde-viven-los-extranjeros-tienen-residencia-chile/'

# Needs Docker => https://www.docker.com/
# Then needs splashr::install_splash()

start_splash()

splash_local %>% 
  splash_response_body(TRUE) %>% 
  splash_go(URL) %>% 
  splash_wait(10) %>% 
  splash_html() -> pg

html_nodes(pg, "div.gig-comment-body")
## {xml_nodeset (10)}
##  [1] <div class="gig-comment-body"><p><span>Algunosdesubicados comentan y se refieren a la UE<span>  </span>como si en alguna forma Chil ...
##  [2] <div class="gig-comment-body">Si buscan información se encontrarán que la unión Europea se está desmorona ndo por asunto de la inmi ...
##  [3] <div class="gig-comment-body">Pocos inmigrantes tiene Chile en función de su población. En España hay 4.5 mill de inmigrantes. 800. ...
##  [4] <div class="gig-comment-body">Chao chilenois idiotas tanto hablan y dicen que hacer cuando ni su pais les pertenece esta gobernado  ...
##  [5] <div class="gig-comment-body">\n<div> Victor Hugo Ramirez Lillo, de Conchalí, exiliado en Goiania, Brasil, pecha bono de exonerado, ...
##  [6] <div class="gig-comment-body">Les escribo desde mi 2do pais, USA.  Mi PDTE. TRUMP  se bajó del  TPP y Chile se va a la cresta. La o ...
##  [7] <div class="gig-comment-body">En CHILE siempre fuimos muy cuidadosos con le emigración, solo lo MEJOR de Alemania, Francia, Suecia, ...
##  [8] <div class="gig-comment-body"><span>Basta de inmigración!!! Santiago está lleno de vendedores ambulantes extranieros!!!¿¿esos son l ...
##  [9] <div class="gig-comment-body">IGNOREN A JON LESCANO, ESE ES UN CHOLO QUE FUE DEPORTADO DE CHILE.<div>IGNOREN A  LOS EXTRANJEROS MET ...
## [10] <div class="gig-comment-body">Me pregunto qué dirá el nacionalista promedio cuando agarre un libro de historia y se dé cuenta de qu ...

killall_splash()

【讨论】:

  • 嗨 hrbrmstr 这听起来是一个很棒的解决方案。我试过splashr,但我得到了这个错误。 start_splash() initialize_python(required_module, use_environment) 中的错误:未找到 Python 安装,未加载 Python 绑定。我的 R 版本是包安装程序更新的 3.4.2
  • 这取决于docker 包。该错误需要对该包进行分类(并且可能需要在此处提交问题github.com/bhaskarvk/docker/issues
猜你喜欢
  • 2021-11-01
  • 1970-01-01
  • 2020-08-28
  • 2019-10-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多