2017-09-03 1 views
-1

여러 페이지 (233)에 걸쳐 Securities Class Action Filings 웹 사이트에서 테이블을 긁어 내려고했습니다. 다음과 같은 오류에rvest package에 오류가 있습니다

install.packages("rvest") 
install.packages("magrittr") 
install.packages("xml2") 

library(xml2) 
library(rvest) 
library(magrittr) 
library(data.table) 


i <- 1:233 
urls <- paste0("http://securities.stanford.edu/filings.html?page=", i) 

get_table <- function(url) { 
    url %>% 
    read_html() %>% 
    html_nodes(xpath = '//*[@id="records"]/table') %>% 
    html_table() 
} 

results <- sapply(urls, get_table) 

코드 결과 :

Error in xpath_element() :
could not find function "xpath_element"

모든 아이디어를 내 코드는 다음과 같다?

을 다시 시작하여 컴퓨터를 다시 시작하고 모든 패키지를 업데이트하려고했습니다.

답변

0

는 R을 다시 설치하고 지금은 코드가 작동하고있는 것입니다. 너희들의 시간을 낭비해서 미안해.

0

나는이 코드가 당신이 필요로하는 것에 가까이 다가 갈 것이라고 생각한다. 하지 아나콘다를 통해이 시간을 - -

suppressPackageStartupMessages(library(tidyverse)) 
suppressPackageStartupMessages(library(rvest)) 


# iterate over the first 10 pages 
iter_page <- 1:10 
pb <- progress_estimated(length(iter_page)) 

# define function to scrape the table data from a page 
get_table <- function(i) { 
    base_url <- "http://securities.stanford.edu/filings.html?page=" 
    url <- paste0(base_url, i) 
    url %>% 
    read_html() %>% 
    html_nodes(xpath = '//*[@id="records"]/table') %>% 
    html_table() %>% 
    .[[1]] %>% 
    as_tibble() 
} 

# scrape first 10 pages 
map_df(iter_page, ~ { 
    pb$tick()$print() 
    df <- get_table(.x) 
    Sys.sleep(sample(10, 1) * 0.1) 
    df 
}) 
#> # A tibble: 200 x 5 
#>              `Filing Name` 
#>                <chr> 
#> 1         Dr. Reddy's Laboratories Ltd. 
#> 2            PetMed Express, Inc. 
#> 3             Top Ships Inc. 
#> 4              Sevcon, Inc. 
#> 5              XCerra Corp. 
#> 6            Zillow Group, Inc. 
#> 7             ShoreTel, Inc. 
#> 8 Teva Pharmaceutical Industries Ltd. : American Depository Shares 
#> 9             Depomed, Inc. 
#> 10          Blue Apron Holdings, Inc. 
#> # ... with 190 more rows, and 4 more variables: `Filing Date` <chr>, 
#> # `District Court` <chr>, Exchange <chr>, Ticker <chr> 
+0

감사합니다. 코드를 실행했지만 여전히 동일한 오류 메시지가 나타납니다. 'map_df (.)'R은 긁기 시작하고 ('| ====== | 10 % ~ 1m remaining') 오류 메시지'Error in xpath_element () : 함수를 찾을 수 없습니다"xpath_element "' –

관련 문제