【问题标题】:Scrolling through entire page with Rselenium, then extracting a tabular data into a data frame使用 Rselenium 滚动整个页面,然后将表格数据提取到数据框中
【发布时间】:2021-11-02 22:16:08
【问题描述】:

我目前正在尝试使用Rseleniumrvesttidyverse 的组合来抓取网站。

目标是转到此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


【解决方案1】:

我解决了这个问题。发生了两件事。第一个是页面自动加载,光标位于搜索栏内。我通过 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()

【讨论】:

    【解决方案2】:

    页面没有向下滚动,因为默认情况下光标在搜索栏中。对您的代码进行了一些修改,使其完全向下滚动。

    #Launch browser
    rD <- rsDriver(browser="firefox", port=9545L, verbose=F)
    remDr <- rD[["client"]]
    remDr$navigate("https://www.pricecharting.com/category/pokemon-cards")
    remDr$findElement(using = "link text", "Promo")$clickElement()
    
    #clicking outside the search bar
    remDr$findElement(using = "xpath", value = '//*[@id="console-page"]')$clickElement()
    
    webElem <- remDr$findElement("css", "body")
    #looping to get at the end of the page. 
    for (i in 1:25){
      Sys.sleep(1)
      webElem$sendKeysToElement(list(key = "end"))
    } 
    
    #extract table
    full_table <- remDr$getPageSource()[[1]]
    html_page <- read_html(full_table)
    html_page %>% 
      html_elements("#games_table") %>% 
      html_table()
    [[1]]
    # A tibble: 888 x 5
       Card                 Ungraded `Grade 9` `PSA 10`  ``                                                                                                        
       <chr>                <chr>    <chr>     <chr>     <chr>                                                                                                     
     1 Mew #8               $3.99    $38.79    $75.62    "+ Collection\n                                        In One Click\n                                    ~
     2 Mewtwo #3            $8.28    $65.91    $227.50   "+ Collection\n                                        In One Click\n                                    ~
     3 Charizard GX #SM211  $7.85    $23.64    $53.50    "+ Collection\n                                        In One Click\n                                    ~
     4 Charizard V #SWSH050 $8.00    $34.99    $79.98    "+ Collection\n                                        In One Click\n                                    ~
     5 Pikachu #24          $138.31  $362.72   $2,919.69 "+ Collection\n                                        In One Click\n                                    ~
     6 Entei #34            $8.50    $52.21    $153.63   "+ Collection\n                                        In One Click\n                                    ~
     7 Ancient Mew          $23.79   $99.99    $382.50   "+ Collection\n                                        In One Click\n                                    ~
     8 Charizard EX #XY121  $27.16   $135.00   $727.00   "+ Collection\n                                        In One Click\n                                    ~
     9 Mewtwo EX #XY107     $5.54    $77.50    $98.71    "+ Collection\n                                        In One Click\n                                    ~
    10 Charizard GX #SM60   $28.57   $113.98   $492.00   "+ Collection\n                                        In One Click\n                                    ~
    # ... with 878 more rows
    

    【讨论】:

    • 有趣的是,您的代码在我的计算机上无法运行。具体来说,webElem$sendKeysToElement(list(key = "end")) 不起作用,而正如我指定的答案代码,remDr$executeScript("window.scrollTo(0,document.body.scrollHeight);") 似乎起作用。但是,我更喜欢你的循环方法,以确保我到达页面底部,而不是我的快速猜测工作。需要注意的一件事。我相信您的解决方案中存在错误,您不小心写了未注释掉的“点击”。
    猜你喜欢
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多