【发布时间】:2020-06-16 14:35:45
【问题描述】:
我正在将 HTML 转换为漂亮整洁的 CSV。我有一个充满表格和类的文件。我有三种类型的表,它们的结构是相同的。唯一的区别是我感兴趣的元素之后的“th”元素中的内容。如何仅获取在“th”(“text_that_I_want_to_get”)中具有特定文本的表的内容?有没有办法在每种类型的表中插入一个带有 R 的类?
表格类型 1
<tr>
<th class="array">text_that_I_want_to_get</th>
<td class="array">
<table>
<thead>
<tr>
<th class="string">name</th>
<th class="string">mean</th>
<th class="string">stdev</th>
</tr>
</thead>
<tbody>
表格类型 2
<tr>
<th class="array">text_that_I_want_to_get</th>
<td class="array">
<table>
<thead>
<tr>
<th class="string">name</th>
<th class="array">answers</th>
</tr>
</thead>
<tbody>
表格类型 3
<tr>
<th class="array">text_that_I_want_to_get</th>
<td class="array">
<table>
<thead>
<tr>
<th class="string">Reference</th>
</tr>
</thead>
<tbody>
【问题讨论】:
-
html 代码是否会在 R 中夹住字符对象?如果是这样你可以试试
sub(".*<th class="array">(.*)</th>.*","\\1", My_HTML_String) -
我认为它不能正常工作。我不仅对“th”(text_that_I_want_to_get)中的内容感兴趣,而且对 tbody 之后的内容感兴趣(这是巨大的,这就是我没有发布它的原因)......而且有很多表格,例如这些,我在 HTML 中没有他们的位置
-
你应该发布你想要的输出。目前尚不清楚您要提取什么。
标签: r web-scraping rvest xml2