【发布时间】:2017-06-02 02:45:20
【问题描述】:
我有一个 html 数据集,如下所示,我想将其解析并转换为可以使用的表格格式。
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<div class="brewery" id="brewery">
<ul class="vcard simple">
<li class="name"> Bradley Farm / RB Brew, LLC</li>
<li class="address">317 Springtown Rd </li>
<li class="address_2">New Paltz, NY 12561-3020 | <a href='http://www.google.com/maps/place/317 Springtown Rd++New Paltz+NY+United States' target='_blank'>Map</a> </li>
<li class="telephone">Phone: (845) 255-8769</li>
<li class="brewery_type">Type: Micro</li>
<li class="url"><a href="http://www.raybradleyfarm.com" target="_blank">www.raybradleyfarm.com</a> </li>
</ul>
<ul class="vcard simple col2"></ul>
</div>
<div class="brewery">
<ul class="vcard simple">
<li class="name">(405) Brewing Co</li>
<li class="address">1716 Topeka St </li>
<li class="address_2">Norman, OK 73069-8224 | <a href='http://www.google.com/maps/place/1716 Topeka St++Norman+OK+United States' target='_blank'>Map</a> </li>
<li class="telephone">Phone: (405) 816-0490</li>
<li class="brewery_type">Type: Micro</li>
<li class="url"><a href="http://www.405brewing.com" target="_blank">www.405brewing.com</a> </li>
</ul>
<ul class="vcard simple col2"></ul>
</div>
</body>
以下是我使用的代码。我面临的问题是它使用 Rvest 转换为文本文件,但似乎无法使其成为任何有用的格式。
library(dplyr)
library(rvest)
url<-html("beer.html")
selector_name<-".brewery"
fnames<-html_nodes(x = url, css = selector_name) %>%
html_text()
head(fnames)
fnames
这是一个正确的方法还是我应该使用其他包来遍历每个 div 和内部元素。
我想看的输出是
No. Name Address Type Website
谢谢。
【问题讨论】:
标签: html r web-scraping rvest