抓取与rvest网站(改变页面,点击链接)
我抓取与rvest的一个研究项目,一个网站,我遇到两个问题:抓取与rvest网站(改变页面,点击链接)
1)我的循环似乎是重复的拼抢一遍又一遍的相同页面,而不是转到以下页面。
2)我无法访问我正在刮的链接的全文。换句话说,我不仅想要刮取搜索结果,而且要显示每个显示的链接的内容。我有代码在每个单独的页面上执行此操作(请参阅下文),但由于有2600个链接,因此我想将它们各自的内容集成到抓取中(就像rvest在每个链接上“点击”并抓取它们内容)。
背景:法国*页面。我正在寻找所有含有“inegalites de sante”字样的内容。这给出了近似2600个结果,每个页面显示30个结果。因此,我运行了88次循环来收集所有结果。然而,它一次又一次地给了我30个相同的结果,而且只是在每个结果的下面,而不是每个话语的全文,都要抄下小文本的引文。
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")
这里是代码,我会用刮每一个人页面以获取全文,以第一话语(没有“103000074”)为例:
#### 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
非常感谢您的帮助!
你可以做到以下几点:
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...
非常感谢!这真的很酷。 (1)用“l_out Evelyne1991
刚编辑我的答案。请按原样进行编码。 @ Evelyne1991我不完全明白你的意思是什么URL ...如果你拿我的'url_base'你看到最后有'sort =&q =&b ='所以我的函数在'sort =&q =&b = 0','sort =&q =&b = 30','sort =&q =&b = 60'等等。这被称为URL参数,并对页面进行分页。因此,例如六页的页面以'b = 150'结尾:http://www.vie-publique.fr/rechercher/recherche.php?query = inegalites%20de%20sante&date =&dateDebut =&dateFin =&skin = cdp&answers = 30&filter =&typeloi =&auteur =&filtreAuteurLibre =&typeDoc =&source =&sort =&q =&b = 150 – Rentrop
非常感谢!它完美的作品。我非常感谢你的帮助。 – Evelyne1991
'sprintf(url_base,1:88)'返回88次相同的URL。你希望'sprintf'能做什么? – MrFlick
@MrFlick我认为它会改变页面88次(如点击“下一步”),这显然不是这里的情况。 – Evelyne1991
一般人会想到硒,如果有人点击链接。 –