我试图在以下位置刮擦表中的所有数据:https://en.wikipedia.org/wiki/List_of_countries_by_firearm-related_death_rate
我尝试使用选择器小工具。实际上,我发现Chrome中的Right-click -> Inspect
选项更易于使用。我发现选择器是:#mw-content-text > div > table.wikitable.sortable.jquery-tablesorter
但是,我得到了不正确的输出character(0)
:
library(rvest)
url <- 'https://en.wikipedia.org/wiki/List_of_countries_by_firearm-related_death_rate'
webpage <- read_html(url)
webpage %>%
html_nodes("#mw-content-text > div > table.wikitable.sortable.jquery-tablesorter") %>%
html_text()
character(0)
我相信这是因为表是由Javascript动态生成的,
rvest
无法读取。我听说RSelenium
可用于下载html,然后可以用上面的rvest
代码进行解析。但是,RSelenium
看起来像是一个小难题(例如,启动服务器,Docker,端口等)。我是否还有另外一个更直观,更容易理解的选项,或者RSelenium确实是我唯一的选择吗?我的目标是编写一份RMarkdown报告,该报告将使用从一个或多个网站抓取的数据构建模型,因此,我希望使用自动Web抓取解决方案。
最佳答案
首先,这是一个HTML表,而不是Javascript表。检查页面时,您可以看到每个表元素,如果它是Javascript表,则不会看到。
在这里使用表xpath
效果很好。检查页面时,可以从右键单击菜单中复制它。
require(rvest)
guns <- url %>% read_html() %>% html_nodes(xpath = '//*[@id="mw-content-text"]/div/table[3]') %>%
html_table()
guns <- guns[[1]]
Country Total Year Homicides Suicides Unintentional Undetermined Sources and notes Guns per 100 inhabitants[citation needed]
1 Argentina ! Argentina 6.36 2009 2.58 (2012) 1.57 (2009) 0.05 (2009) 2.57 (2009) Guns in Argentina[1][2] 10.2
2 Australia ! Australia 0.93 2013 0.16 (2013) 0.74 (2013) 0.02 (2013) 0.02 (2013) Guns in Australia[3] 21.7
3 Austria ! Austria 2.63 2011 0.10 (2011) 2.43 (2011) 0.01 (2009) 0.04 (2011) Guns in Austria[4] 30.4
4 Azerbaijan ! Azerbaijan 0.30 ? 0.27 (2010) 0.01 (2007) 0.02 (2007) ? Guns in Azerbaijan[5] 3.5
5 Barbados ! Barbados 3.12 ? 3.12 (2013) ? ? ? Guns in Barbados[6] 7.8
6 Belarus ! Belarus 0.23 ? 0.14 (2009) ? 0.09 (1996) ? Guns in Belarus[7] 7.3
还有一些清理工作要做,这是一个国家的例子:
require(dplyr)
guns <- guns %>% mutate(Country = trimws(gsub("!.*","", Country)))