我一直在使用rvest
来抓取页面,我知道selectorGadget
的好处。 但是,一个页面包含没有选择器的数据。下面是一个 HTML 片段。 页面在这里 . 我正在尝试抓取列出的每张爵士专辑的人员名单。在下面的HTML片段中,人事数据以"Sonny Rollins,男高音萨克斯......">开头 如您所见,该文本没有被任何CSS选择器包围。有什么建议吗?
<h1>Blue Note Records Catalog: 4000 series</h1>
<div id="start-here"><!-- id="start-here" --></div>
<div id="catalog-data">
<h2>Modern Jazz 4000 series (12 inch LP)</h2>
<h3><a href="./album-index/#blp-4001" name="blp-4001">BLP 4001 Sonny
Rollins - Newk's Time <i>1959</i></a></h3>
Sonny Rollins, tenor sax; Wynton Kelly, piano #1,2,4-6; Doug Watkins, bass
#1,2,4-6; Philly Joe Jones, drums.
<div class="date">Van Gelder Studio, Hackensack, NJ, September 22,
1957</div>
<table width="100%">
<tr><td width="15%">1. tk.5<td>Tune Up
等。。。
使用 xpath 提取并使用正则表达式过滤掉元素。以下脚本应该有效。
library(rvest)
library(stringr)
texts <- read_html("https://www.jazzdisco.org/blue-note-records/catalog-4000-series/") %>%
html_nodes(xpath = '//*[@id="catalog-data"]/text()') %>%
html_text()
texts[!str_detect(texts,"(^\n$)|(^\n\*\*)")] # I just notcie this line doesn't clean up the string entirely, you can figure out better regex.
关于拆分字符串,您可以尝试以下代码:
sample_str <- "nIke Quebec, tenor sax; Sonny Clark, piano; Grant Green, guitar; Sam Jones, bass; Louis Hayes, drums.n"
str_trim(sample_str) %>%
str_split(",")
返回:
[[1]]
[1] "Ike Quebec" " tenor sax; Sonny Clark" " piano; Grant Green" " guitar; Sam Jones" " bass; Louis Hayes" " drums."