0
我有一个html数据集,如下所示,我想解析并转换成可以使用的表格格式。使用R解析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
这将是一个正确的做法或者我应该使用一些其他的包都要经过每格和内部元素做。
的放出来,我想看看它是
No. Name Address Type Website
谢谢。
非常感谢@austensen。我得到的唯一的错误是在整个文件中为类型运行这个错误。在我们尝试替换空白类型值时会做些什么。 '错误:列'类型'必须是长度1或7263,而不是7147' – SNT
哦,听起来像,与你的例子不同,有一些啤酒厂在你的真实数据中缺少类型字段,您的数据帧长度不同。我不得不多想一些如何解决这个问题。 – austensen