【发布时间】:2018-11-17 08:07:24
【问题描述】:
我的问题
我正在尝试从此 URL 中抓取文档:
url <- https://www.bger.ch/ext/eurospider/live/de/php/aza/http/index.php?lang=de&type=simple_query&query_words=&lang=de&top_subcollection_aza=all&from_date=01.01.2017&to_date=05.01.2017&x=0&y=0
单个感兴趣文档的代码如下所示:
<span class="rank_title">
<a href="https://www.bger.ch/ext/eurospider/live/de/php/aza/http/index.php?lang=de&type=highlight_simple_query&page=1&from_date=01.01.2017&to_date=05.01.2017&sort=relevance&insertion_date=&top_subcollection_aza=all&query_words=&rank=5&azaclir=aza&highlight_docid=aza%3A%2F%2F05-01-2017-2C_826-2015&number_of_ranks=67" title="Seite mit hervorgehobenen Suchbegriffen öffnen">05.01.2017 2C 826/2015</a>
</span>
<span class="published_info small normal">
<a href="https://www.bger.ch/ext/eurospider/live/de/php/aza/http/index.php?lang=de&type=highlight_simple_query&page=1&from_date=01.01.2017&to_date=05.01.2017&sort=relevance&insertion_date=&top_subcollection_aza=all&query_words=&highlight_docid=atf%3A%2F%2F143-I-73%3Ade&azaclir=aza">publiziert</a>
</span>
<div class="rank_data">
<div class="court small normal">
IIe Cour de droit public
</div>
<div class="subject small normal">
Finances publiques & droit fiscal
</div>
<div class="object small normal">
Impôts communal et cantonal 2009, impôt sur la fortune; estimation de titres non cotés, garantie de la propriété
</div>
</div> </li>
我对课程感兴趣:"rank_title", "published info small normal",
"subject small normal" 和 "object small normal"。
我想将这些信息存储在数据框中。
但是,并非所有文档都具有所有类(例如,在此页面上,只有一个文档具有 "published info small normal" 类。
如果"published info small normal"可用,我主要感兴趣的是提取该文档的标题,在这个例子中:
143 一世 73
编辑
如果脚本仅在"published info small normal" 可用时提取“publiziert”,那就没问题了。
我的方法
我发现一篇似乎对我的问题非常有用的帖子 Scraping with rvest - complete with NAs when tag is not present
我开始实现这个:
library(XML)
doc <- xmlTreeParse(url, asText = TRUE, useInternalNodes = TRUE)
但是,我不知道如何实现变量节点的代码。
【问题讨论】:
标签: r xml web-scraping rvest