使用 xpathSApply 的相同代码搜索多个路径
Searching for multiple paths using same code for xpathSApply
我正在尝试提取包含阿拉伯语诗歌的 table。您可以在 here
查看这首诗
我试图解析 table...
URL <- "http://www.adab.com/modules.php?name=Sh3er&doWhat=shqas&qid=65546&r=&rc=1"
Data <- htmlTreeParse(URL, useInternalNodes = TRUE,encoding = "Windows-1256")
Poem <- xpathSApply(Data,"//p[@class='poem']",xmlValue)
Poem1 <- xpathSApply(Data,"//font[@class='poem']",xmlValue)
Encoding(Poem) <- "UTF-8"
Encoding(Poem1) <- "UTF-8"
但这不好,因为我改变了写诗的顺序。
那么,有没有办法只使用一个代码来获得 table 中所写的 URL 呢?
例如:
Poem <- xpathSApply(Data,"//p[@class='poem']&//font[@class='poem']",xmlValue)
问题实际上是关于适当 select 或获取 class 为 "poem" 的多个标签。有几个选项。一个简单的选项是在 XPath select 或:
中为标签名称使用通配符 *
Poem <- xpathSApply(Data,"//*[@class='poem']",xmlValue)
如果您只想要 class "poem"
的 p
和 font
标签,而不是,请说 [=37] 的 div
标签=],您可以使用 |
(或)运算符来 select 多个选项。翻译成 rvest
,我觉得它更容易阅读(尽管相同的 selector 在 xpathSApply
中也能正常工作):
library(rvest)
Poem <- URL %>% read_html() %>%
html_nodes(xpath = '//p[@class="poem"] | //font[@class="poem"]') %>%
html_text(trim = TRUE)
如果使用 rvest
,另一种选择是使用 CSS select 或者代替 XPath。在CSS中,class由.
指定,所以通配符版本只需要".poem"
;要限制为仅 p
或 font
标签,请使用 "p.poem, font.poem"
。 Here's a fun tutorial on CSS selectors, if you like.
Poem <- URL %>% read_html() %>%
html_nodes(css = '.poem') %>%
html_text(trim = TRUE)
head(Poem, 15) # I don't speak Arabic, so check that the results make sense
## [1] "أقداح و أحلام" "أنا لا أزال و في يدي قدحي" "ياليل أين تفرق الشرب"
## [4] "ما زلت أشربها و أشربها" "حتى ترنح أفقك الرحب" "الشرق عُفر بالضباب فما"
## [7] "يبدو فأين سناك يا غرب؟" "ما للنجوم غرقن ، من سأم" "في ضوئهن و كادت الشهب ؟"
## [10] "أنا لا أزال و في يدي قدحي" "ياليل أين تفرق الشرب ؟" "******"
## [13] "الحان بالشهوات مصطخب" "حتى يكاد بهن ينهار" "و كأن مصاحبيه من ضرج"
我正在尝试提取包含阿拉伯语诗歌的 table。您可以在 here
查看这首诗我试图解析 table...
URL <- "http://www.adab.com/modules.php?name=Sh3er&doWhat=shqas&qid=65546&r=&rc=1"
Data <- htmlTreeParse(URL, useInternalNodes = TRUE,encoding = "Windows-1256")
Poem <- xpathSApply(Data,"//p[@class='poem']",xmlValue)
Poem1 <- xpathSApply(Data,"//font[@class='poem']",xmlValue)
Encoding(Poem) <- "UTF-8"
Encoding(Poem1) <- "UTF-8"
但这不好,因为我改变了写诗的顺序。
那么,有没有办法只使用一个代码来获得 table 中所写的 URL 呢?
例如:
Poem <- xpathSApply(Data,"//p[@class='poem']&//font[@class='poem']",xmlValue)
问题实际上是关于适当 select 或获取 class 为 "poem" 的多个标签。有几个选项。一个简单的选项是在 XPath select 或:
中为标签名称使用通配符*
Poem <- xpathSApply(Data,"//*[@class='poem']",xmlValue)
如果您只想要 class "poem"
的 p
和 font
标签,而不是,请说 [=37] 的 div
标签=],您可以使用 |
(或)运算符来 select 多个选项。翻译成 rvest
,我觉得它更容易阅读(尽管相同的 selector 在 xpathSApply
中也能正常工作):
library(rvest)
Poem <- URL %>% read_html() %>%
html_nodes(xpath = '//p[@class="poem"] | //font[@class="poem"]') %>%
html_text(trim = TRUE)
如果使用 rvest
,另一种选择是使用 CSS select 或者代替 XPath。在CSS中,class由.
指定,所以通配符版本只需要".poem"
;要限制为仅 p
或 font
标签,请使用 "p.poem, font.poem"
。 Here's a fun tutorial on CSS selectors, if you like.
Poem <- URL %>% read_html() %>%
html_nodes(css = '.poem') %>%
html_text(trim = TRUE)
head(Poem, 15) # I don't speak Arabic, so check that the results make sense
## [1] "أقداح و أحلام" "أنا لا أزال و في يدي قدحي" "ياليل أين تفرق الشرب"
## [4] "ما زلت أشربها و أشربها" "حتى ترنح أفقك الرحب" "الشرق عُفر بالضباب فما"
## [7] "يبدو فأين سناك يا غرب؟" "ما للنجوم غرقن ، من سأم" "في ضوئهن و كادت الشهب ؟"
## [10] "أنا لا أزال و في يدي قدحي" "ياليل أين تفرق الشرب ؟" "******"
## [13] "الحان بالشهوات مصطخب" "حتى يكاد بهن ينهار" "و كأن مصاحبيه من ضرج"