# Load Packages
pacman::p_load(tidyverse, rvest)
# Set URL
url <- "https://www.worldometers.info/coronavirus/"
website <- read_html(url)
# Scrape Cases Data
cases_html <- html_nodes(website, "td.sorting_1")
cases <- html_text(cases_html)
cases_html
cases
我正在嘗試使用 rvest 抓取 webdata,但是當我在此處檢查我的兩個變數(“cases_html”和“cases”)時出現以下錯誤。每個的輸出分別是:
> {xml_nodeset (0)}
> character(0)
我不知道為什么我沒有從這個網站上得到任何資料。我也嘗試過使用 RSelenium 包,就像在另一篇文章中推薦的那樣,但該代碼也因不相關的錯誤而失敗。我認為解決方案應該在 Rvest 中可用,但是,我想弄清楚這里到底出了什么問題。
uj5u.com熱心網友回復:
目前尚不清楚您要從頁面中抓取什么,但您可以像這樣獲取主資料表:
library(tidyverse)
library(rvest)
read_html("https://www.worldometers.info/coronavirus/") %>%
html_nodes("#main_table_countries_today") %>%
html_table() %>%
pluck(1)
#> # A tibble: 244 x 22
#> `#` `Country,Other` TotalCases NewCases TotalDeaths NewDeaths
#> <int> <chr> <chr> <chr> <chr> <chr>
#> 1 NA "North America" 98,313,200 " 23,167" 1,459,752 " 147"
#> 2 NA "Asia" 147,921,193 " 130,458" 1,423,876 " 395"
#> 3 NA "South America" 56,801,380 " 17,492" 1,294,318 " 31"
#> 4 NA "Europe" 191,122,646 " 187,856" 1,817,850 " 587"
#> 5 NA "Oceania" 7,156,060 " 46,381" 10,626 " 59"
#> 6 NA "Africa" 11,902,057 " 6,666" 253,795 " 4"
#> 7 NA "" 721 "" 15 ""
#> 8 NA "World" 513,217,257 " 412,020" 6,260,232 " 1,223"
#> 9 1 "USA" 83,055,836 " 18,777" 1,020,749 " 89"
#> 10 2 "India" 43,079,157 " 3,293" 523,803 ""
#> # ... with 234 more rows, and 16 more variables: TotalRecovered <chr>,
#> # NewRecovered <chr>, ActiveCases <chr>, `Serious,Critical` <chr>,
#> # `Tot Cases/1M pop` <chr>, `Deaths/1M pop` <chr>, TotalTests <chr>,
#> # `Tests/1M pop` <chr>, Population <chr>, Continent <chr>,
#> # `1 Caseevery X ppl` <chr>, `1 Deathevery X ppl` <chr>,
#> # `1 Testevery X ppl` <int>, `New Cases/1M pop` <chr>,
#> # `New Deaths/1M pop` <dbl>, `Active Cases/1M pop` <chr>
由reprex 包于 2022-04-30 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/469438.html
下一篇:R中的資料框架摘要統計
