方式1
给自己的浏览器装个XPATH
点击XPATH
然后写入://*/div[1]/div/div/div/div/ul/li[2]/a
直接复制右边的33篇文章标题,存为TCGA_33_title.txt即可
方式2
打开网页:https://www.cell.com/pb-assets/consortium/pancanceratlas/pancani3/index.html
右键点击网页另存
然后用task4b_Nature_PAN_Cancer_词云
里面同样的方法解析网页即可
urlpage <- XML::htmlParse('Welcome to the Pan-Cancer Atlas.html') #解析xml
title = XML::xpathSApply(urlpage, '//*/div[1]/div/div/div/div/ul/li[2]/a')
title=lapply(title, function(name){name[[1]]})
title
sink("TCGA_33_title.txt")
for (data in title) {
print(data)
#产生了一个特殊空格<U+00A0>,这个是空格的特殊格式;使用特殊字符进行表达\u00A0$
}
sink()
file <- scan('TCGA_33_title.txt',sep='\n',what='',encoding="UTF-8")
txtList = lapply(file, strsplit,"\\s+") #使用空格符号进行分词
txtChar = tolower(unlist(txtList)) #把所有单词变为小写模式
txtChar <- txtChar[nchar(txtChar)>2] #去除字符长度小于2的词语
txtChar <- table(txtChar) #统计词频
#grepl类似grep,但是返回逻辑向量,即是否包含pattern
txtChar <- txtChar[!grepl('^[0-9-,]+$',names(txtChar),perl = TRUE)] #去除纯数字
txtChar <- txtChar[!grepl('^and|the|of$',names(txtChar),perl = TRUE)] #delete and the,of
data=data.frame(txtChar)
wordcloud::wordcloud(words = data$txtChar, freq = data$Freq, min.freq = 1,
max.words=200, random.order=FALSE, rot.per=0.35,
colors=RColorBrewer::brewer.pal(8, "Dark2"))