【发布时间】:2021-11-02 22:16:08
【问题描述】:
我目前正在尝试使用Rselenium、rvest 和tidyverse 的组合来抓取网站。
目标是转到此this website,单击其中一个链接(例如,“促销”),然后使用rvest 提取整个数据表(例如,卡片和分级价格) .
使用以下代码,我能够毫无问题地提取表格:
library(RSelenium)
library(rvest)
library(tidyverse)
pokemon <- read_html("https://www.pricecharting.com/console/pokemon-promo")
price_table <- pokemon %>%
html_elements("#games_table") %>%
html_table()
但是,这有几个问题:1) 我无法浏览我提供的初始网站链接 (https://www.pricecharting.com/category/pokemon-cards) 上的所有不同卡片组,以及 2) 我无法使用这种方法提取整个表格 - 只能主要加载什么。
为了缓解这些问题,我正在调查Rselenium。我决定去最初的网站,点击卡片组的链接(例如“促销”),然后加载整个页面。此工作流程可在此处显示:
## open driver
rD <- rsDriver(browser="firefox", port=4545L, verbose=F)
remDr <- rD[["client"]]
## navigate to primary page
remDr$navigate("https://www.pricecharting.com/category/pokemon-cards")
## click on the link I want
remDr$findElement(using = "link text", "Promo")$clickElement()
## find the table
table <- remDr$findElement(using = "id", "games_table")
## load the entire table
table$sendKeysToElement(list(key = "end"))
## get the entire source
full_table <- remDr$getPageSource()[[1]]
## read in the table
html_page <- read_html(full_table)
## Do the `rvest` technique I had above.
html_page %>%
html_elements("#games_table") %>%
html_table()
但是,我的问题是我再次得到相同的 51 个元素而不是整个表格。
我想知道是否可以将我的两种技术结合起来,以及在我的编码过程中哪里出了问题。
【问题讨论】:
-
整个页面加载不出来,解压表格前尝试滚动到底部。
-
这能回答你的问题吗? Scrolling page in RSelenium
-
不,我在最初的帖子中使用了类似的东西。 This question and answer 最终解决了这个问题。但是,仍然存在光标在搜索栏中的问题,所以我不得不“点击”出来。
标签: r web-scraping rvest rselenium