2017-10-21 51 views

回答

1

使用xpath您可以先以类名称infobox获取信息框,然后通过其标记名称a获取所有链接。

library("rvest") 

url <- "https://en.wikipedia.org/wiki/New_York_City" 
infobox <- url %>% 
    read_html() %>% 
    html_nodes(xpath='//table[contains(@class, "infobox")]//a') 

print(infobox) 

输出

{xml_nodeset (81)} 
[1] <a href="/wiki/City_(New_York)" class="mw-redirect" title="City (New York)">City</a> 
[2] <a href="/wiki/File:NYC_Montage_2014_4_-_Jleon.jpg" class="image" title="Clockwise, from top: Midtow ... 
[3] <a href="/wiki/Midtown_Manhattan" title="Midtown Manhattan">Midtown Manhattan</a> 
[4] <a href="/wiki/Times_Square" title="Times Square">Times Square</a> 
[5] <a href="/wiki/Unisphere" title="Unisphere">Unisphere</a> 
... 
相关问题