网页抓取多个级别的网站

时间:2018-03-06 09:04:38

标签: r web-scraping rvest

我正在寻找一个网站。然后,对于每个已删除的项目,我想在子网页上抓取更多信息。作为一个例子,我将使用IMDB网站。我正在使用List<TestClass> result = list.stream() .map( s -> s.toString().equals("bbb") ? new TestClass("DDD") : s) .collect(Collectors.toList()); 包和Google Chrome中的selector gadget

从IMDB网站我可以得到top 250 rated TV shows,如下所示:

rvest

列表中的前250部电影中的每一部都是可点击的链接,可提供有关每部电影的其他信息。在这种情况下,对于library('rvest') # url to be scrapped url <- 'http://www.imdb.com/chart/toptv/?ref_=nv_tp_tv250_2' #Reading the HTML code from the website webpage <- read_html(url) #Using CSS selectors to scrap movies_html <- html_nodes(webpage,'.titleColumn a') #Converting the TV show data to text movies <- html_text(movies_html) head(movies) [1] "Planet Earth II" "Band of Brothers" "Planet Earth" "Game of Thrones" "Breaking Bad" "The Wire" 中的每部电影,我还要抓取演员并将其存储在另一个movies中。例如,如果你点击第二部到顶部的电影“Band of brothers”并向下滚动,演员包括来自Scott Grimes和Phil McKee的约40人。

我想做的伪代码:

list

我确信这很简单但对我来说是新的,我不知道如何寻找合适的术语来寻找解决方案。

1 个答案:

答案 0 :(得分:1)

如果我理解正确,你正在寻找一种方法

  1. 从前250(main_url
  2. 中识别电影页面的网址
  3. 获取前250名演出(m_titles

  4. 的标题
  5. 访问这些网址(m_urls

  6. 摘录这些电视节目(m_cast
  7. 的演员表

    正确?

    我们首先定义一个从电视节目页面中提取演员表的功能:

    getcast <- function(url){
      page <- read_html(url)
      nodes <- html_nodes(page, '#titleCast .itemprop')
      cast <- html_text(nodes)
    
      inds <- seq(from=2, to=length(cast), by=2)
      cast <- cast[inds]
      return(cast)
    }
    

    有了这个,我们可以解决第1点到第4点:

    # Open main_url and navigate to interesting part of the page:
    main_url <- "http://www.imdb.com/chart/toptv/?ref_=nv_tp_tv250_2"
    
    main_page <- read_html(url)
    movies_html <- html_nodes(main_page, '.titleColumn a')
    
    # From the interesting part, get the titles and URLs:
    m_titles <- html_text(movies_html)
    
    sub_urls <- html_attr(movies_html, 'href')
    m_urls <- paste0('http://www.imdb.com', sub_urls)
    
    # Use `getcast()` to extract movie cast from every URL in `m_urls`
    m_cast <- lapply(m_urls, getcast)
    
相关问题