首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >有没有一种方法可以在一个网站上抓取多个页面

有没有一种方法可以在一个网站上抓取多个页面
EN

Stack Overflow用户
提问于 2021-04-06 21:04:31
回答 2查看 127关注 0票数 0

我是R和webscraping的新手。在实践中,我试图从一个有多个页面的假网站('http://books.toscrape.com/catalogue/page-1.html')上抓取书名,然后根据书名计算特定的指标。每页有20本书和50页,我已经设法收集并计算了前20本书的指标,但是我想计算网站上全部1000本书的指标。

当前输出如下所示:

代码语言:javascript
运行
复制
 [1] "A Light in the Attic"                                                                          
 [2] "Tipping the Velvet"                                                                            
 [3] "Soumission"                                                                                    
 [4] "Sharp Objects"                                                                                 
 [5] "Sapiens: A Brief History of Humankind"                                                         
 [6] "The Requiem Red"                                                                               
 [7] "The Dirty Little Secrets of Getting Your Dream Job"                                            
 [8] "The Coming Woman: A Novel Based on the Life of the Infamous Feminist, Victoria Woodhull"       
 [9] "The Boys in the Boat: Nine Americans and Their Epic Quest for Gold at the 1936 Berlin Olympics"
[10] "The Black Maria"                                                                               
[11] "Starving Hearts (Triangular Trade Trilogy, #1)"                                                
[12] "Shakespeare's Sonnets"                                                                         
[13] "Set Me Free"                                                                                   
[14] "Scott Pilgrim's Precious Little Life (Scott Pilgrim #1)"                                       
[15] "Rip it Up and Start Again"                                                                     
[16] "Our Band Could Be Your Life: Scenes from the American Indie Underground, 1981-1991"            
[17] "Olio"                                                                                          
[18] "Mesaerion: The Best Science Fiction Stories 1800-1849"                                         
[19] "Libertarianism for Beginners"                                                                  
[20] "It's Only the Himalayas"

我希望这是1000本书的长度而不是20本,这将允许我使用相同的代码来计算指标,但对于1000本书而不是20本。

代码:

代码语言:javascript
运行
复制
url<-'http://books.toscrape.com/catalogue/page-1.html'

url %>%
  read_html() %>%
  html_nodes('h3 a') %>%
  html_attr('title')->titles
titles

什么是从网站上抓取每本书的最好方法,并使列表中的书名从20本减少到1000本?提前谢谢。

EN

回答 2

Stack Overflow用户

发布于 2021-04-06 21:21:47

生成50个URL,然后对其进行迭代,例如使用purrr::map

代码语言:javascript
运行
复制
library(rvest)

urls <- paste0('http://books.toscrape.com/catalogue/page-', 1:50, '.html')

titles <- purrr::map(
  urls, 
  . %>% 
    read_html() %>%
    html_nodes('h3 a') %>%
    html_attr('title')
)
票数 1
EN

Stack Overflow用户

发布于 2021-04-06 21:21:11

也许是这样的东西?

代码语言:javascript
运行
复制
library(tidyverse)
library(rvest)
library(data.table)
# Vector with URL's to scrape
url <- paste0("http://books.toscrape.com/catalogue/page-", 1:20, ".html")
# Scrape to list
L <- lapply( url, function(x) {
  print( paste0( "scraping: ", x, " ... " ) )
  data.table(titles = read_html(x) %>%
              html_nodes('h3 a') %>%
              html_attr('title') )
})
# Bind list to single data.table
data.table::rbindlist(L, use.names = TRUE, fill = TRUE)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66969215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档