【问题标题】:How to scrape description of first link from Google?如何从谷歌抓取第一个链接的描述?
【发布时间】:2017-08-09 02:02:30
【问题描述】:

我关注了:How to get google search results

它有效,但我想抓取谷歌返回的第一个链接的描述。对于CRAN 关键字,它是:

<span class="st"><em>CRAN</em> is a network of ftp and web servers around the world that store identical, up-to-date, versions of code and documentation for R. Please use the <em>CRAN</em>&nbsp;...</span>

但我不知道这里的span部分是什么,请提供不使用RSelenium的解决方案

【问题讨论】:

    标签: r web-scraping


    【解决方案1】:

    使用rvest:

    library(rvest)
    
    baseUrl <- 'https://www.google.it/search?q='
    
    query = 'cran'
    url <- paste0(baseUrl, query)
    
    
    read_html(url) %>% 
        html_nodes('.st') %>% 
        # This select only the first result, change number to select another reusult
        # or comment it to get all first page results
        '['(2) %>% 
        html_text()
    

    【讨论】:

      【解决方案2】:

      您可以从 Google 知识图(Google 搜索结果页面右侧的摘要框)中抓取。

      您可以为此使用 Google Knowledge Graph API:

      1. 在 Google Developers Console 中创建应用程序

      2. 创建身份验证凭据

        knowlegdegraph<-function(query)
        {
           API_Key<-"Your_API_KEY"
           url<-paste("https://kgsearch.googleapis.com/v1/entities:search?query=",query, 
             "&key=", API_Key,
             "&limit=1&indent=True")
          jdata <- fromJSON(URLencode(url))
        
        } 
        

      jdata 是一个列表。您可以使用以下方法提取描述的 JSON 元素:

      简要说明:

      jdata[["itemListElement"]][["result"]][["description"]]
      

      详细说明:

      jdata[["itemListElement"]][["result"]][["detailedDescription"]][["articleBody"]]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-18
        • 1970-01-01
        • 1970-01-01
        • 2023-04-06
        • 2022-10-04
        • 1970-01-01
        • 2022-11-10
        • 1970-01-01
        相关资源
        最近更新 更多