3

今日、Web サイトからデータをスクレイピングするために RSelenium を初めて使用しました。タブとドロップダウン メニューを使用して必要なデータに移動できますが (難しいですか?)、必要な実際のデータを抽出する段階で行き詰まります (簡単な問題です!)。

これまでの私のコードは次のとおりです。

library(RSelenium)
checkForServer()
startServer()
remDr <- remoteDriver$new()
remDr$open()
remDr$navigate("https://www.whoscored.com/Teams/31")
webElem1 <- remDr$findElement(value = '//a[@href = "#team-squad-stats-detailed"]')
webElem1$clickElement()
webElem2 <- remDr$findElement("id", "category")
webElem2$clickElement()
webElem2$sendKeysToElement(list(key="down_arrow", key="down_arrow", key="down_arrow",
 key="down_arrow", key="down_arrow", key="enter"))
webElem3 <- remDr$findElement("id", "subcategory")
webElem3$clickElement()
webElem3$sendKeysToElement(list(key="down_arrow", key="enter"))
webElem4 <- remDr$findElement("id", "statsAccumulationType")
webElem4$clickElement()
webElem4$sendKeysToElement(list(key="down_arrow", key="down_arrow", key="down_arrow",
 key="enter"))
webElem5 <- remDr$findElement("id", "player-table-statistics-body")

このプレーヤーテーブルのデータをcsv形式に抽出する最も簡単な方法を教えてください。XML パッケージと readHTMLTable を使用して他の (静的) Web サイトをスクレイピングすることに慣れていますが、これを上記の RSelenium の手順と組み合わせる方法に行き詰まっています。

ありがとうございました

編集 - 新鮮な目でこれに戻ってきたので、私が見つけた答えは以下のとおりです。

webElem5 <- remDr$findElement(using = "id", value = "statistics-table-detailed")
webElem5txt <- webElem5$getElementAttribute("outerHTML")[[1]]
table <- readHTMLTable(webElem5txt, header=TRUE, as.data.frame=TRUE)[[1]]

これにより、Web サイトのこの部分で必要なことを進めることができます。

よろしければ、同じサイトの別の部分についてお尋ねしたいと思います。次のように、必要なデータに移動します。

remDr$navigate("https://www.whoscored.com/Matches/959894")
webElem1 <- remDr$findElement(using = "link text", value = "Match Centre")
webElem1$clickElement()
webElem2 <- remDr$findElement(value = '//a[@href = "#chalkboard"]')
webElem2$clickElement()

抽出したいデータはこれらのボックスにありますが、HTML にはテーブルとして構築されているとは書かれていないので、どのように処理すればよいかわかりません。

ここに画像の説明を入力

4

0 に答える 0