我一直在使用rvest
来抓取页面,我知道selectorGadget
的好处。但是,一个页面的数据没有选择器。下面是HTML的片段。页面is here。我试图在每张爵士专辑中列出人员名单。在下面的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
等等...
答案 0 :(得分:1)
使用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."