【发布时间】:2021-12-31 18:18:32
【问题描述】:
也许同样的问题:R - Scraping an HTML table with rvest when there are missing <tr> tags
这是数据。数据的扩展名以 XLS 文件的形式给出。 我可以通过 read_html 读取这些数据,而不是 read_xml
当我使用 read_xml 时出现错误。 错误消息是“开始和结束标签不匹配:tbody 第 41 行和 tr [76]”
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml" lang="ko">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<style type="text/css">
td {
text-align: center;
}
</style>
</head>
<body>
<table cellpadding="0" cellspacing="0" border="1" summary="summary">
<thead>
<tr>
<th>v1</th>
<th>v2</th>
<th>v3</th>
<th>v4</th>
<th>v5</th>
</tr>
</thead>
<tbody>
<td>aa1</td>
<td>aa2</td>
<td>aa3</td>
<td>aa4</td>
<td>aa5</td>
</tr>
</tbody>
</table>
</body>
</html>
我的代码和结果
tmp<-read_html('file_name') %>% html_table()
> [[1]]
[1] V1 V2 V3 V4 V5
<0 rows> (or 0-length row.names).
为什么它不能读取 'td'?
期望的输出
v1 v2 v3 v4 v5
aa1 aa2 aa3 aa4 aa5
还有,
tmp %>% html_nodes('table') %>% htmlTableWidget
此代码可以正确识别数据帧。 但我需要数据框结果,而不是小部件。
【问题讨论】: