用rvest刮网站(更改页面,点击链接)

时间:2017-04-04 03:00:24

标签: r web-scraping rvest

我正在用rvest搜索一个研究项目的网站,我遇到两个问题:

1)我的循环似乎一遍又一遍地重复同一页面上的抓取,而不是继续下一页。

2)我无法在我正在抓取的链接中访问全文。换句话说,我不仅要搜索搜索结果,还要搜索每个显示链接的内容。我有代码在每个单独的页面上执行此操作(见下文),但由于有2600个链接,我想将他们的个人内容整合到抓取中(好像rvest是"点击"每个这些链接和抓取他们的内容)。

背景:法国政府页面。我正在寻找所有含有" inegalites de sante"的内容。这给出了大约2600个结果,每个页面显示30个结果。因此,我运行循环88次以收集所有结果。然而,它一遍又一遍地给我30个相同的结果,并且只是在每个结果下刮取小文本引用,而不是每个话语的全文。

请参阅网站:http://www.vie-publique.fr/rechercher/recherche.php?replies=30&query=inegalites+de+sante&typeloi=&filter=&skin=cdp&date=1&auteur=&source=&typeDoc=&date=&sort=&filtreAuteurLibre=&dateDebut=&dateFin=&nbResult=2612&q=

library(rvest)
library(purrr)

url_base <- "http://www.vie-publique.fr/rechercher/recherche.php?replies=30&query=inegalites+de+sante&typeloi=&filter=&skin=cdp&date=1&auteur=&source=&typeDoc=&date=&sort=&filtreAuteurLibre=&dateDebut=&dateFin=&nbResult=2612&q="

map_df(1:88, function(i) {

  # Progress indicator
  cat(".")

  pg <- read_html(sprintf(url_base, i))

  data.frame(date=html_text(html_nodes(pg, ".date")),
             text=html_text(html_nodes(pg, ".recherche_montrer")),
             title=html_text(html_nodes(pg, ".titre a")),
             stringsAsFactors=FALSE)

}) -> viepublique_data

dplyr::glimpse(viepublique_data)

write.xlsx(viepublique_data, "/Users/Etc.Etc./viepublique_data.xlsx")

以下是我用来刮取每个页面以获取全文的代码,以第一个话语(不是#34; 103000074&#34;)为例:

#### Code to scrape each individual page

website <- read_html("http://discours.vie-publique.fr/notices/103000074.html")

section <- website %>% 
  html_nodes(".level1 a") 
section

subsection <- website %>% 
  html_nodes(".level2 p") 
subsection

person <- website %>% 
  html_nodes("p:nth-child(2) , .article p:nth-child(1)") 
person

text <- website %>% 
  html_nodes(".col1 > p") 
text

title <- website %>% 
  html_nodes("h2") 
title

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:3)

您可以执行以下操作:

require(rvest)
require(tidyverse)
require(stringr)

# The url parameter of interest is the "b" at the end
# it is used for pagination. Just plut in ther 30*(0:87) to get
# the urls of your 88 pages
url_base <- "http://www.vie-publique.fr/rechercher/recherche.php?query=inegalites%20de%20sante&date=&dateDebut=&dateFin=&skin=cdp&replies=30&filter=&typeloi=&auteur=&filtreAuteurLibre=&typeDoc=&source=&sort=&q=&b="
l_out <- 88
urls <- paste0(url_base, seq(0, by = 30, length.out = l_out))

定义帮助函数来抓取网站:

# Helper function for parsing overview
parse_overview <- function(x){
  tibble(date = html_text(html_nodes(x, ".date"), TRUE),
         text_1 = html_text(html_nodes(x, ".recherche_montrer"), TRUE),
         title = html_text(html_nodes(x, ".titre a"), TRUE),
         link = str_trim(html_attr(html_nodes(x, ".titre a"), "href")))
}

# Helper function for collapse multi-line output like person and text
collapse_to_text <- function(x){
  p <- html_text(x, trim = TRUE)
  p <- p[p != ""] # drop empty lines
  paste(p, collapse = "\n")
}

# Parse the result itself
parse_result <- function(x){
  tibble(section = html_text(html_node(x, ".level1 a"), trim = TRUE),
         sub_section = html_text(html_node(x, ".level2 a"), trim = TRUE),
         person = html_nodes(x, "p:nth-child(2) , .article p:nth-child(1)") %>% collapse_to_text,
         text_2 = html_nodes(x, ".col1 > p") %>% collapse_to_text)
}

实际抓取工作如下:

# Scrape overview    
overview_content <- urls %>% 
  map(read_html) %>% 
  map_df(parse_overview)

# scrape all pages - that may take a while... slow website
detail_content <- overview_content$link %>% 
  map(read_html) %>% 
  map_df(parse_result)

out <- bind_cols(overview_content, detail_content)

这会给你

Variables: 8
$ date        <chr> "11/01/2010", "06/02/2014", "31/03/2011", "30/08/2010", "21/09/2010", "19/05/2010"
$ text_1      <chr> "En effet, l' inégalité d'information n'est pas le moindre déterminant des inégalités de santé",...
$ title       <chr> "1 - Déclaration de Mme Roselyne Bachelot-Narquin, ministre de la santé et des sports, sur la ré...
$ link        <chr> "http://discours.vie-publique.fr/notices/103000074.html", "http://discours.vie-publique.fr/notic...
$ section     <chr> "Discours publics", "Discours publics", "Discours publics", "Discours publics", "Discours public...
$ sub_section <chr> "Les discours dans l'actualité", "Les discours dans l'actualité", "Les discours dans l'actualité...
$ person      <chr> "Personnalité, fonction : BACHELOT-NARQUIN Roselyne.\nFRANCE. Ministre de la santé et des sports...
$ text_2      <chr> "ti : Madame la ministre, chère Fadela,Monsieur le directeur général de la santé, cher Didier Ho...
相关问题