【问题标题】:get links while do web scraping to google in R在 R 中对谷歌进行网页抓取时获取链接
【发布时间】:2019-07-19 22:38:44
【问题描述】:

我正在尝试在搜索时获取google的链接,即所有这些链接:。

我已经完成了这种抓取,但在这种情况下,我不明白为什么它不起作用,所以我运行以下几行:

library(rvest)
url<-"https://www.google.es/search?q=Ediciones+Peña+sl+telefono"
content_request<-read_html(url)
content_request %>%
    html_nodes(".r") %>%
    html_attr("href")

我尝试过其他节点,得到了类似的答案:

content_request %>%
    html_nodes(".LC20lb") %>%
    html_attr("href")

最后我尝试获取网页的所有链接,但有一些链接无法下载:

html_attr(html_nodes(content_request, "a"), "href")

请问,在这种情况下您能帮帮我吗?谢谢。

【问题讨论】:

    标签: r web-scraping rvest


    【解决方案1】:

    这里有两个选项供您使用。

    #1) 
    
    url <- "https://www.google.es/search?q=Ediciones+Pe%C3%B1a+sl+telefono"
    html <- paste(readLines(url), collapse="\n")
    library(stringr)
    matched <- str_match_all(html, "<a href=\"(.*?)\"")
    
    
    #2) 
    
    library(xml2)
    library(rvest)
    URL <- "https://www.google.es/search?q=Ediciones+Pe%C3%B1a+sl+telefono"
    pg <- read_html(URL)
    head(html_attr(html_nodes(pg, "a"), "href"))
    

    【讨论】:

    • 在第一种情况下,您获得的链接比显示的多,而在第二种情况下,您获得的链接比显示的少。
    猜你喜欢
    • 1970-01-01
    • 2020-08-31
    • 2018-08-01
    • 1970-01-01
    • 2018-09-13
    • 2020-08-08
    • 1970-01-01
    • 2014-12-28
    相关资源
    最近更新 更多