微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

css – 从R中的链接中提取标题

我正在用R中的rvest包练习网页抓取.到目前为止,这是一个很棒的指南. ( http://zevross.com/blog/2015/05/19/scrape-website-data-with-the-new-r-package-rvest/).使用工具选择器小工具我可以识别对我想要的项目的类或div元素引用(据我所知).

所以我刚去维基百科,并试图提取美国总统的名单.该页面链接https://en.wikipedia.org/wiki/List_of_Presidents_of_the_United_States.Selector Gadget告诉我元素类/ div / ???? (不知道该怎么称呼它)是“大a”.

到目前为止,这是我的代码

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函数获取一个元素?我觉得我很亲密!

感谢您的任何帮助.

解决方法

名字有两个来源. title属性和文本.它们的格式可能略有不同,或者可能包含中间首字母或其他内容.使用你最喜欢的那个.

html_attr(fnames,“title”)

要么

HTML_TEXT(fnames)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。