我試圖找出每個口袋妖怪(第一代)可以學習的動作數量。
我找到了包含此資訊的以下網站:
最后,我想在前面的資料框中添加一列,其中包含每個 Pokemon 可以學習的移動數。例如,看起來像這樣的東西:
> head(pokemon_websites)
template_1 names template_2 full_website number_of_moves
1 https://pokemondb.net/pokedex/ Bulbasaur /moves/1 https://pokemondb.net/pokedex/Bulbasaur/moves/1 24
2 https://pokemondb.net/pokedex/ Ivysaur /moves/1 https://pokemondb.net/pokedex/Ivysaur/moves/1 ???
3 https://pokemondb.net/pokedex/ Venusaur /moves/1 https://pokemondb.net/pokedex/Venusaur/moves/1 ???
4 https://pokemondb.net/pokedex/ Charmander /moves/1 https://pokemondb.net/pokedex/Charmander/moves/1 ???
5 https://pokemondb.net/pokedex/ Charmeleon /moves/1 https://pokemondb.net/pokedex/Charmeleon/moves/1 ???
6 https://pokemondb.net/pokedex/ Charizard /moves/1 https://pokemondb.net/pokedex/Charizard/moves/1 ???
- 有沒有辦法在 R 中抓取這些資料,計算 150 個口袋妖怪中每一個的移動次數,然后將此移動計數放入一列?
現在我正在手動執行此操作,并且需要很長時間!另外,我聽說有些網站不允許自動網頁抓取 - 如果這個網站 ( https://pokemondb.net/pokedex/game/red-blue-yellow ) 不允許網頁抓取,我可以嘗試找到另一個可能的網站允許它。
謝謝!
uj5u.com熱心網友回復:
您可以使用以下方法刮取每個 pokemen 的所有表格:
tables =lapply(pokemon_websites$full_website,function(link) {
tryCatch(
read_html(link) %>% html_nodes("table") %>% html_table(),
error = function(e) {}, warning=function(w) {}
)
})
但是,請注意每個口袋妖怪回傳的表格數量不同。例如,第一個有 6 張桌子——前三個是紅色/藍色的,后三個是黃色的。
lengths(tables)
[1] 6 6 6 6 6 6 6 6 6 2 4 7 2 4 8 6 6 6 4 4 6 6 6 6 6 8 6 6 0 4 8 4 8 6 8 4 6 6 8 4 4 6 6 8 6 6 5 5 5 5 4 4 6 6 6
[56] 6 4 6 6 6 8 6 6 6 6 6 6 6 6 8 6 6 6 6 6 4 4 6 6 6 6 0 6 6 6 6 4 4 6 8 4 4 6 6 6 6 6 6 6 6 4 8 6 7 6 6 6 4 4 6
[111] 6 6 6 6 6 6 6 6 6 8 0 6 4 6 6 6 6 2 8 6 2 4 8 8 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
uj5u.com熱心網友回復:
由于 OP 只想計算Red/Blue選項卡中的移動,我們可以執行以下操作,(如果您需要從兩個選項卡移動,請遵循@langtang 答案)
tables1 =lapply(pokemon_websites$full_website, function(x){
tryCatch( x %>% read_html() %>% html_nodes('.active') %>% html_nodes('.resp-scroll') %>% html_table(),
error = function(e) NULL
)
})
moves= lapply(tables1, function(x) lapply(x, function(x) dim(x)[1]))
moves = lapply(moves, unlist, use.names=FALSE)
moves = lapply(moves, sum) %>% unlist()
[1] 24 25 27 32 33 37 32 33 37 2 3 30 2 3 26 22 23 25 24 27 21 23 24 26 29 30 27 29 0 28 43 28 44 41 42 22 23 40 41 19 22 21 23 25 23 26 22 29 20 23 24
[52] 27 31 34 31 34 23 25 25 36 37 25 34 35 29 31 32 23 24 26 28 31 30 31 33 22 26 37 46 23 26 0 23 26 25 28 22 24 27 29 20 20 32 25 33 36 25 27 24 27 24 29
[103] 32 35 26 26 37 19 21 27 42 44 24 36 22 24 25 27 33 34 0 21 34 35 30 23 27 2 34 34 1 19 32 32 28 30 22 28 22 30 24 43 25 25 22 30 32 36 45 60
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/456925.html
上一篇:分配前參考的變數“html”:UnboundLocalError
下一篇:使用R(rvest)進行網頁抓取
