2016-06-02 5 views
0

ウェブページには、ウェブページに1つのセルに複数の要素を持つ種類のテーブルがあります。私は次のコードでテーブルのコンテンツをクロールできますが、これらの要素をWebページアーキテクチャとしてバインドできませんでした。これらの要素を完全に組み合わせるためのいくつかの方法がありますか、または各要素を得るために他のアイデアを使用する必要がありますか?クロール中に複数の要素を持つテーブルのアーキテクチャを1つのセルに保持するR

library(XML) 
dataissued <- "http://www.irgrid.ac.cn/handle/1471x/294320/browse?type=dateissued" 
    ec_parsed <- htmlTreeParse(dataissued, encoding = "UTF-8", useInternalNodes = TRUE) 

# gether content in table and build the dataframe 
# title and introduction link of IR resource 
item_title <- xpathSApply(ec_parsed, '//td[@headers="t1"]//a', xmlValue) 
item_hrefs <- xpathSApply(ec_parsed, '//td[@headers="t1"]//a/@href') 
# author and introduction link of IR resource 
auth_name <- xpathSApply(ec_parsed, '//td[@headers="t2"]//a', xmlValue) 
auth_hrefs <- xpathSApply(ec_parsed, '//td[@headers="t2"]//@href') 
# publish date of IR resource 
pub_date <- xpathSApply(ec_parsed, '//td[@headers="t3"]', xmlValue) 
# whole content link of IR resource 
con_link <- xpathSApply(ec_parsed, '//td[@headers="t3"]//a[@href]', xmlValue) 

item_table <- cbind(item_title, item_hrefs, auth_name, auth_hrefs, pub_date, con_link) 
colnames(item_table) <- c("t1", "href1", "t2", "href2", "t3", "t4", "href4") 

私は何度も試してみましたが、まだそれがあるべきように、1枚の紙が複数の著者を持っていること、およびすべての作者とそのリンクが一つの「行」に保存する必要がありますが、今は1本の著者と同じように、それらを整理することができません紙のタイトルは完全に再利用されています。それは結果を台無しにする。

+0

私は 'con_link'を空にして、最後の行に6列を持つitem_tableに7つのcolnamesを割り当てようとするとエラーになります。コードを修正してください。 – xxfelixxx

+0

それは残念です。私は、自分のコードは、それが鳴っているデータを組み合わせる上でいくつかのエラーがあると説明していましたが、私はそれを修正しようとしましたが、何も動作しませんでした。これに注目していただきありがとうございます。 –

答えて

0

これは、そのテーブルから長いデータフレームを作るための一つの方法である:

library(rvest) 
library(purrr) 
library(tibble) 

pg <- read_html("http://www.irgrid.ac.cn/handle/1471x/294320/browse?type=dateissued") 

# extract the columns 

col1 <- html_nodes(pg, "td[headers='t1']") 
col2 <- html_nodes(pg, "td[headers='t2']") 
col3 <- html_nodes(pg, "td[headers='t3']") 

# this is the way to get the full text column 

col4 <- html_nodes(pg, "td[headers='t3'] + td") 

# now, iterate over the rows; map_df() will bind all our data.frame's together 

map_df(1:legnth(col1), function(i) { 

    # extract the links 

    a1 <- xml_nodes(col1[i], "a") 
    a2 <- xml_nodes(col2[i], "a") 
    a4 <- xml_nodes(col4[i], "a") 

    # put the row into a long data.frame for the row 

    data_frame(  title = html_text(a1, trim=TRUE), 
       title_link = html_attr(a1, "href"), 
        author = html_text(a2, trim=TRUE), 
      author_link = html_attr(a2, "href"), 
       issue_date = html_text(col3[i], trim=TRUE), 
       full_text = html_attr(a4, "href")) 

}) 
+0

温かくご協力いただきありがとうございます。上記のプログラムで使用したパッケージを一度も使用していないことは非常に恥ずかしいことです。そしてあなたの素敵なコードは本当に素晴らしいです。しかし、私はまだ2つの質問があります。 1. "map_df"は "関数"であり、それと結合されたデータフレームは一般的な方法では公開できないだけでなく、 Rstudioのコンソールにエコーされたデータフレームのコンテンツにはマスコードが含まれています。 2.これらの要素を結合した後、生成されたデータフレームの読み取り方法を変更する必要がありますか? –

0

「rvest」パッケージを使用した時の最大の問題は、混乱のコードです。パラメータ "encoding"もプログラムで使用されていても、結果にはまだ混乱コードがあります。しかし、WebページのエンコーディングはUTF-8です。以下のような:私のテストのために

library(rvest) 
pg <- read_html("http://www.irgrid.ac.cn/handle/1471x/294320/browse?type=dateissued", encoding = "UTF-8") 

、最高のパフォーマンスは、「XML」、私はgetNodeset機能を使用する場合、結果は右である、まったく混乱コードでなければなりません。しかし、私はノード全体を取得するだけで、テーブルの各行をその構造でゲッタリングできませんでした。

library(XML) 
pg <- "http://www.irgrid.ac.cn/handle/1471x/294320/browse?type=dateissued" 
pg_tables <- getNodeSet(htmlParse(pg), "//table[@summary='This table browse all dspace content']") 
# gether the node of whole table 
papernode <- getNodeSet(pg_tables[[1]], "//td[@headers='t1']") 
paper_hrefs <- xpathSApply(papernode[[1]], '//a/@href') 
paper_name <- xpathSApply(papernode[[1]], '//a', xmlValue) 
# gether authors in table 
authnode <- getNodeSet(pg_tables[[1]], "//td[@headers='t2']") 
# gether date in table 
datenode <- getNodeSet(pg_tables[[1]], "//td[@headers='t3']") 

このプログラムでは、これらの「ノード」を別々に取得できました。しかし、ヘッダーとそのリンクをクロールするのが難しくなっているようです。 "getNodeSet"の結果クラスは "html_nodes"と同じではないためです。 「getNodeSet」によって生成されたデータフレームを自動的に読み取って、これらのノードからヘッダーとそのリンクを正確に抽出するにはどうすればよいですか?

関連する問題