gpt4 book ai didi

r - 使用 Rselenium 滚动浏览整个页面,然后将表格数据提取到数据框中

转载 作者:行者123 更新时间:2023-12-05 05:53:50 24 4
gpt4 key购买 nike

我目前正在尝试结合 Rseleniumrvesttidyverse 来抓取网站。

目标是去这个this website ,单击其中一个链接(例如,“Promo”),然后使用 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。我决定去最初的网站,点击卡片组的链接(例如“Promo”),然后加载整个页面。此工作流程可在此处显示:

## 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 个元素而不是整个表格。

我想知道是否可以结合我的两种技术,以及我的编码过程中哪里出了问题。

最佳答案

我解决了这个问题。有两件事正在发生。首先是页面自动加载,光标位于搜索栏内。我通过执行 remDr$findElement(using = "css", "body")$clickElement() 点击文本正文来摆脱这个问题。接下来,作为一个great question/answer指出,如果滚动/箭头键不能与 sendKeysToElement(list(key = "up_arrow")) 一起使用,您应该尝试 remDr$executeScript("window.scrollTo(0,document) .body.scrollHeight);").

因此,我的脚本的一个小示例如下:

library(RSelenium)
library(rvest)
library(tidyverse)

## opens the driver
rD <- rsDriver(browser="firefox", port=4545L, verbose=F)
remDr <- rD[["client"]]

link_texts <- c("Base Set", "Promo", "Fossil")
## navigates to the correct page
remDr$navigate("https://www.pricecharting.com/category/pokemon-cards")

for (name in link_texts) {
## finds the link and clicks on it
remDr$findElement(using = "link text", name)$clickElement()
## gets the table path
remDr$findElement(using = "css", "body")$clickElement()
## finds the table - this line may be extraneous
table <- remDr$findElement(using = "css", "body")
## scrolls to the bottom of the table
remDr$executeScript("window.scrollTo(0,document.body.scrollHeight);")
Sys.sleep(1)
remDr$executeScript("window.scrollTo(0,document.body.scrollHeight);")
Sys.sleep(1)
remDr$executeScript("window.scrollTo(0,document.body.scrollHeight);")
Sys.sleep(1)
remDr$executeScript("window.scrollTo(0,document.body.scrollHeight);")
Sys.sleep(1)
remDr$executeScript("window.scrollTo(0,document.body.scrollHeight);")
Sys.sleep(1)
remDr$executeScript("window.scrollTo(0,document.body.scrollHeight);")
Sys.sleep(1)
## get the entire page source that's been loaded
html <- remDr$getPageSource()[[1]]
## read in the page source
page <- read_html(html)

data_name <- str_to_lower(str_replace(name, " ","_"))
## extract the tabular table
df <- page %>%
html_elements("#games_table") %>%
html_table() %>%
pluck(1) %>%
select(1:4)
assign(data_name, df)
Sys.sleep(3)
remDr$navigate("https://www.pricecharting.com/category/pokemon-cards")
}

## close driver
remDr$close()
rD$server$stop()

关于r - 使用 Rselenium 滚动浏览整个页面,然后将表格数据提取到数据框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69817527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com