2017-05-08 6 views
0

私は、清掃されたxpathを数千回の繰り返しの間にthe Vermont Secretaty of Stateから削り取るための効率的なソリューションを探しています。 、大きなデータ(〜90k)XPathの掻き取り

'//*[@id="content_wrapper"]/div[2]/div/h1' 

私は約90000ページをループループを実行するために、クリーンで効率的な方法を見つけるのに苦労してるタイトルをつかみ、そして格納し:これは私がこすりしようとしているタイトルのXPathでありますベクトルで。最終目標は、ページの値とタイトルxpathを含む小さなデータフレームをエクスポートすることです。このデータフレームを使用して、データベース内の将来の検索に索引付けします。

これは私がこれまで持っているものです。残念ながら

library(XML) 
library(rvest) 

election_value <- 1:90000 
title <- NA 

for (i in 1:90000) { 
    url <- sprintf("http://vtelectionarchive.sec.state.vt.us/elections/view/%s", election_value[i]) 
    if (is.null(tryCatch({read_html(url) %>% html_nodes(xpath='//*[@id="content_wrapper"]/div[2]/div/h1') %>% html_text()}, error=function(e){}))) { 
    title[i] <- NA } else { 
     title[i] <- read_html(url) %>% html_nodes(xpath='//*[@id="content_wrapper"]/div[2]/div/h1')} 
} 
vermont_titles <- data.frame(election_value, title) 
write.csv(vermont_titles, 'vermont_titles.csv') 

html_nodes()関数ではなく単なるテキストより、括弧で文字列を返すので、スクリプトは動作しません。このスクリプトは1週間ほど私を迷惑にしていたので、どんな解決策もありがたいです。

+0

あなたが投稿したURLを確認してください、「http://vtelectionarchive.sec.state.vt.us/elections/ view /%s "は、' 400 Bad request'を返します。私は正しいURLがhttp://vtelectionarchive.sec.state.vt.us/elections/search/year_from:1789/year_to:2016 – Ashish

+0

であると考えています。 '%s'は数字のb/cに置き換えられています'sprintf()'呼び出しです。 OPが何をしようとしているのかはまだ分かりません。 – hrbrmstr

答えて

2

ここでは動作する解決策があります。詳細についてはコメントを参照してください:

library(rvest) 

#url<-"http://vtelectionarchive.sec.state.vt.us/elections/view/68156" 
election_value <- 68150:68199 

#predefine title vector 
title <- vector("character", length=length(election_value)) 

for (i in 1:50) { 
    url <- sprintf("http://vtelectionarchive.sec.state.vt.us/elections/view/%s", election_value[i]) 
    #read page and test if null 
    page<-tryCatch({read_html(url)}, error=function(e){}) 
    if (is.null(page)) 
    { 
     title[i] <- NA } 
    else { 
    #parse the page and extract the title as text 
    node<-page %>% html_nodes(xpath='//*[@id="content_wrapper"]/div[2]/div/h1') 
    title[i] <- node %>% html_text() 
    } 
} 
vermont_titles <- data.frame(election_value, title) 
write.csv(vermont_titles, 'vermont_titles.csv') 

ノートのカップルを:パフォーマンスを向上させるだけで1時間を2回に分けて、一度の代わりに最大のページを読み、ページを解析します。また、タイトルをベクトルとして事前定義することは、パフォーマンスをさらに向上させることです。

1

もう一つの解決策は次のようになります。

require(tidyverse) 
require(rvest) 
election_value <- c(3,68150:68153) 
base_url <- "http://vtelectionarchive.sec.state.vt.us/elections/view/" 
urls <- paste0(base_url, election_value) 

map(urls, possibly(read_html, NA_character_)) %>% 
    map_if(negate(is.na), html_nodes, xpath = '//*[@id="content_wrapper"]/div[2]/div/h1') %>% 
    map_if(negate(is.na), html_text) %>% 
    as.character %>% 
    tibble(election_value, title = .) 

返す:

# A tibble: 5 × 2 
    election_value             title 
      <dbl>             <chr> 
1    3             NA 
2   68150 2014 Probate Judge General Election Rutland County 
3   68151 2014 Probate Judge General Election Orleans County 
4   68152 2014 Probate Judge General Election Grand Isle County 
5   68153 2014 Probate Judge General Election Lamoille County 
関連する問題