首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rvest返回NA

rvest返回NA
EN

Stack Overflow用户
提问于 2017-01-26 14:15:53
回答 2查看 762关注 0票数 1

我正在使用"rvest“进行网页抓取,但我无法从页面提取模型的价格:- https://www.motorola.com/us/products/moto-z-force-droid-edition。我需要从页面中提取"$720.00“。我的代码是:

代码语言:javascript
复制
library(rvest)

data<-read_html("https://www.motorola.com/us/products/moto-z-force-droid-edition")

price<-data%>%
html_nodes(".price-amount")%>%
html_text()
print(price)

我不断地从价格中得到字符(0)。

请帮帮忙。

EN

回答 2

Stack Overflow用户

发布于 2017-02-02 06:18:02

代码语言:javascript
复制
url<-"https://www.motorola.com/us/js/motorola_blc_catalog/price/1281"
page<-html_session(url)
text<-XML::xmlToList(XML::xmlParse(httr::content(page$response)$price))
price<-text$tbody$tr$td$span$text
票数 1
EN

Stack Overflow用户

发布于 2018-02-08 13:50:37

将rvest与RSelenium和seleniumPipes一起使用。确保下载phantomJS并将.exe文件放在当前R工作目录中。详情请参见my other answser

代码语言:javascript
复制
library(tidyverse)
library(rvest)
library(RSelenium) # start a server with utility function
library(seleniumPipes)
#start server
rD <- rsDriver (browser = 'chrome',chromever = "latest",port = 4445L)
#open browser
remDr <- remoteDr(browserName = "chrome",port = 4445L)
web_url <- "https://www.motorola.com/us/products/moto-z-force-droid-edition"

remDr %>% go(web_url)
price <- remDr %>% getPageSource() %>% html_nodes(".price-amount") %>% html_text()

print(price)    
[1] "$30.00/mo" "$720.00"   "$720.00" 

remDr %>% deleteSession() # delete session
rD[["server"]]$stop() # close server

简而言之,您需要使用无头浏览器来呈现javascript生成的值。在此之后,您可以照常使用rvest。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41867823

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档