【发布时间】:2016-10-07 19:58:46
【问题描述】:
我正在使用 R 中的 rvest 包练习网页抓取。到目前为止,此页面是一个很好的指南。 (http://zevross.com/blog/2015/05/19/scrape-website-data-with-the-new-r-package-rvest/)。使用工具 Selector Gadget 我可以识别我想要的项目的类或 div 元素引用(据我所知)。
所以我只是去了维基百科,并试图提取美国总统的名单。该页面的链接是https://en.wikipedia.org/wiki/List_of_Presidents_of_the_United_States。 Selector Gadget 告诉我元素 class/div/???? (不知道怎么称呼它)是“大”。
到目前为止,这是我的代码:
site = read_html("https://en.wikipedia.org/wiki/List_of_Presidents_of_the_United_States")
fnames = html_nodes(site,"big a")
部分输出是:
{xml_nodeset (44)}
[1] <a href="/wiki/George_Washington" title="George Washington">George Washington</a>
[2] <a href="/wiki/John_Adams" title="John Adams">John Adams</a>
[3] <a href="/wiki/Thomas_Jefferson" title="Thomas Jefferson">Thomas Jefferson</a>
[4] <a href="/wiki/James_Madison" title="James Madison">James Madison</a>
[5] <a href="/wiki/James_Monroe" title="James Monroe">James Monroe</a>
[6] <a href="/wiki/John_Quincy_Adams" title="John Quincy Adams">John Quincy Adams</a>
[7] <a href="/wiki/Andrew_Jackson" title="Andrew Jackson">Andrew Jackson</a>
[8] <a href="/wiki/Martin_Van_Buren" title="Martin Van Buren">Martin Van Buren</a>
太棒了!所以我提取了带有链接的名称!我只想要名字,所以我不知道如何在这里继续。有没有办法轻松获取链接 html 代码之间的名称?或者我应该使用 html_nodes 函数来获取另一个元素吗?我觉得我很接近了!
感谢您的帮助。
【问题讨论】:
-
html_text(fnames)应该这样做。 -
心都碎了。那行得通!非常感谢!!!
-
或者...
html_attr(fnames, "title")