首页 > 解决方案 > 通过 Google Playstore 在 R 中进行网页抓取

问题描述

我想从谷歌游戏商店中抓取我想要的几个应用程序评论的数据。

  1. 名称字段

  2. 他们得了多少星

  3. 他们写的评论

这是senerio的快照

#Loading the rvest package
library('rvest')

#Specifying the url for desired website to be scrapped
url <- 'https://play.google.com/store/apps/details?id=com.phonegap.rxpal&hl=en_IN'

#Reading the HTML code from the website
webpage <- read_html(url)

#Using CSS gradient_Selector to scrap the name section
Name_data_html <- html_nodes(webpage,'.kx8XBd .X43Kjb')

#Converting the Name data to text
Name_data <- html_text(Name_data_html)

#Look at the Name
head(Name_data)

但结果是

> head(Name_data)

character(0)

后来我尝试发现更多我发现 Name_data_html 有

> Name_data_html
{xml_nodeset (0)}

我是网络抓取的新手,可以帮助我解决这个问题!

标签: rweb-scrapingrvestdata-extraction

解决方案


您应该使用 Xpaths 来选择网页上的对象:

#Loading the rvest package
library('rvest')
#Specifying the url for desired website to be scrapped
url <- 'https://play.google.com/store/apps/details?id=com.phonegap.rxpal&hl=en_IN'
#Reading the HTML code from the website
webpage <- read_html(url)
# Using Xpath
Name_data_html <- webpage %>% html_nodes(xpath='/html/body/div[1]/div[4]/c-wiz/div/div[2]/div/div[1]/div/c-wiz[1]/c-wiz[1]/div/div[2]/div/div[1]/c-wiz[1]/h1/span')
#Converting the Name data to text
Name_data <- html_text(Name_data_html)
#Look at the Name
head(Name_data)

看看如何获​​取这张图片中的路径:

在此处输入图像描述


推荐阅读