首页 > 解决方案 > rvest 不会超过一定数额

问题描述

我正在尝试抓取此网站 url= https://www.kimovil.com/en/compare-smartphones/f_min_dm+unveileddate.3,i_b+slug.samsung

我正在使用 rvest 来抓取这个网站。这是我正在使用的代码。

site <- 'https://www.kimovil.com/en/compare-smartphones/f_min_dm+unveileddate.3,i_b+slug.samsung'
website <- read_html(site)

device_label_html <- html_nodes(website,'div.device-name')
device_label <- html_text(device_label_html)
head(device_label,n=60)

运行此代码后,它最多需要 40 个结果(电话),尽管它应该是 51 个结果(电话)。有人可以帮我解决这个问题。谢谢你。

标签: rweb-scrapingrvest

解决方案


该网站是内部分页的。可能有一种更优雅的方式来做到这一点。如果它超过 2 页,我肯定会找一个,但这有效:

library(rvest)
site <- 'https://www.kimovil.com/en/compare-smartphones/f_min_dm+unveileddate.3,i_b+slug.samsung'
website <- read_html(site)

device_label_html <- html_nodes(website,'div.device-name')
device_label <- html_text(device_label_html)

site2 <- 'https://www.kimovil.com/en/compare-smartphones/f_min_dm+unveileddate.3,i_b+slug.samsung,page.2'
website2 <- read_html(site2)

device_label_html2 <- html_nodes(website2,'div.device-name')
device_label2 <- html_text(device_label_html2)

head(c(device_label, device_label2),n=60)

推荐阅读