【发布时间】:2017-08-24 13:26:17
【问题描述】:
我的问题
我正在尝试将 HTML 文件(通过 Google Drive API 下载为 text/html)解析为 R 中的列表。
HTML 看起来像这样(对德语内容感到抱歉):
<p style='padding:0;margin:0;color:#000000;font-size:11pt;font-
family:"Arial";line-height:1.15;orphans:2;widows:2;text-align:left'>
<span>text: Das </span>
<span style="color:#1155cc;text-decoration:underline"><a
href="https://www.google.com/url?q=http://www.bundesverfassungsgericht.de/SharedDocs/Entscheidungen/DE/2011/10/rs20111012_2bvr023608.html&sa=D&ust=1503574789125000&usg=AFQ
jCNE4Ij3mvMX-QttYQYqspAaMxaZaeg" style="color:inherit;text-
decoration:inherit">Verfassungsgericht urteilt</a></span>
<span style='color:#000000;font-weight:400;text-
decoration:none;vertical-align:baseline;font-size:11pt;font-
family:"Arial";font-style:normal'>,
dass eindeutig private Kommunikation von der Überwachung ausgenommen
sein muss</span></p>
当我尝试使用以下方法从 xmlValues(XML 库)中提取文本时效果很好:
doc <- htmlParse(html, asText = TRUE)
text <- xpathSApply(doc, "//text()", xmlValue)
但就我而言,我需要在 HTML 文件中保留链接 (<a>-tags),并删除 https://www.google.com/url?q=-部分。所以我想摆脱所有样式,只保留文本+链接标签。
到目前为止我尝试了什么
我尝试通过在 XPath 中使用 //(p | a) 来获取这两个节点,但没有成功。
【问题讨论】: