使用 XML 包将 html 表抓取到 R 数据帧中
如何使用 XML 包抓取 html 表?
以巴西足球队的维基百科页面为例。我想在 R 中阅读它并获取“巴西与 FIFA 认可球队对阵的所有比赛列表”表作为数据框。我该怎么做?
How do I scrape html tables using the XML package?
Take, for example, this wikipedia page on the Brazilian soccer team. I would like to read it in R and get the "list of all matches Brazil have played against FIFA recognised teams" table as a data.frame. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
…或更短的尝试:
所选表格是页面上最长的表格
…or a shorter try:
the picked table is the longest one on the page
编辑添加:
示例输出
Edited to add:
Sample output
rvest
和xml2
是另一个用于解析 html 网页的流行包。该语法比
xml
包更易于使用,并且对于大多数网页,该包提供了人们需要的所有选项。The
rvest
along withxml2
is another popular package for parsing html web pages.The syntax is easier to use than the
xml
package and for most web pages the package provides all of the options ones needs.另一种选择是使用 Xpath。
产生这个结果
Another option using Xpath.
Produces this result