r - 有没有办法从网站上的多个页面中抓取,但在 R 中保持我的计算指标相同?
问题描述
我是 R 和网络抓取的新手。为了练习,我一直在抓取一个假书网站('http://books.toscrape.com/catalogue/page-1.html'),然后根据我抓取的信息计算指标。到目前为止,我已经设法计算出第一页的正确指标(主要感谢你们)。但是,这个网站有 50 页,每页都包含 20 本书。我现在想扩展我的度量计算,以包括网页上的所有 1000 本书。顺便说一下,指标是:
- 书名中每个单词的平均长度。
- 最常用的词包括停用词。
- 最常用的词,不包括停用词。
- “和”这个词在书名中出现了多少次。
我设法刮掉了每一页的书名。但是,我之前计算的指标现在不起作用。
以前的代码(一页):
url<-paste0('http://books.toscrape.com/catalogue/page-1.html')
url %>%
read_html() %>%
html_nodes('h3 a') %>%
html_attr('title')->titles
titles
# Mean lengths of words in book titles:
mean(nchar(unlist(strsplit(titles, '\\s+'))))
# Most used word (Including stop words):
mostUsedWord<-head(sort(table(tolower(unlist(strsplit(titles, '\\s+')))), decreasing = TRUE))[1]
mostUsedWord # 'the' - Used 14 times
# Most used word (excluding stop words)
all_words <- tolower(unlist(strsplit(titles, '\\s+')))
noStopWords <- head(sort(table(all_words[!all_words %in% tm::stopwords()]), decreasing = TRUE))[1:3]
noStopWords
# How many times the word 'and' Appears:
word = 'and'
data.frame(word = table(all_words)) %>%
dplyr::filter(word.all_words == word)
输出(书籍清单):
[1]《阁楼上的一盏灯》
[2]《倾诉天鹅绒》
[3]《Soumission》
[4] 《利器》[
5]《智人:人类简史》
[6]《安魂曲红》 ”
[7] “获得理想工作的肮脏小秘密”
[8] “即将到来的女人:根据臭名昭著的女权主义者维多利亚伍德哈尔的生活改编的小说”
[9] “船上的男孩:九个美国人和他们在 1936 年柏林奥运会上对金牌的史诗般的追求”[10]“黑玛丽亚”
[11]“饥饿的心(三角贸易三部曲,#1)”
[12]“莎士比亚十四行诗”
[13]“让我自由”
[ 14]“斯科特朝圣者”s Precious Little Life (Scott Pilgrim #1)”
[15] “撕掉它,重新开始”
[16]“我们的乐队可能是你的生活:来自美国独立地下的场景,1981-1991”
[17] “Olio”
[18] “Mesaerion:1800-1849 年最好的科幻小说”
[19] “初学者的自由主义”
[20] “只有喜马拉雅山”
上面的版本在首页输出书籍的正确值。但是,当我尝试合并所有页面时,计算完全混乱,在尝试计算平均值时出现错误,例如:“strsplit(titles, "\s+") 中的错误:非字符参数”。
多页代码:
url<-paste0('http://books.toscrape.com/catalogue/page-', 1:50, '.html')
# Scraping the book titles and storing them appropriately:
titles <- purrr::map(
url,
. %>%
read_html() %>%
html_nodes('h3 a') %>%
html_attr('title')
)
titles
# Mean lengths of words in book titles:
mean(nchar(unlist(strsplit(titles, '\\s+'))))
# Now outputs "Error in strsplit(titles, "\s+") : non-character argument"
# Most used word (Including stop words):
mostUsedWord<-head(sort(table(tolower(unlist(strsplit(titles, '\\s+')))), decreasing = TRUE))[1]
mostUsedWord # Still outputs the initial 1 page value (I want it for all pages)
# Most used word (excluding stop words)
all_words <- tolower(unlist(strsplit(titles, '\\s+')))
noStopWords <- head(sort(table(all_words[!all_words %in% tm::stopwords()]), decreasing = TRUE))[1:3]
noStopWords # Still outputs the initial 1 page value (I want it for all pages)
# How many times the word 'and' Appears:
word = 'and'
data.frame(word = table(all_words)) %>%
dplyr::filter(word.all_words == word)
# Still outputs the initial 1 page value (I want it for all pages)
输出(书籍列表):同上,但 50 个书籍列表(1-20)
我想知道是否有更好的方法可以从网站上的所有页面中抓取,从而使我能够计算相同的指标。还是我必须完全重写计算指标的代码?简而言之,我想知道是否有一种方法可以计算网站上每个页面的上述指标。提前致谢。
解决方案
推荐阅读
- javascript - 借助 iOS Safari 上的 getUsermedia 访问麦克风
- python - 如何使用 Python 从链接中解析表
- javascript - Javascript:更新数组中嵌套对象的属性会更新所有相似对象的相同属性
- swift - 快速处理段落
- php - 试图获取非对象的属性(查看:C:\xampp\htdocs\Ticket\resources\views\user\user_view_table.blade.php)
- javascript - 如何在状态下制作动态参数名
- xamarin.android - 如何绑定到 Spinner.SelectedItem
- javascript - Bootstrap 模式中的 jQuery 验证不会触发
- node.js - NODEJS mongodb如何将变量放入查询中
- ansible - 如何在 Ansible 中选择主机时使用条件