如何获得 google 个搜索结果

How to get google search results

我使用了以下代码:

library(XML)
library(RCurl)
getGoogleURL <- function(search.term, domain = '.co.uk', quotes=TRUE) 
    {
    search.term <- gsub(' ', '%20', search.term)
    if(quotes) search.term <- paste('%22', search.term, '%22', sep='') 
        getGoogleURL <- paste('http://www.google', domain, '/search?q=',
        search.term, sep='')
    }

    getGoogleLinks <- function(google.url) 
    {
       doc <- getURL(google.url, httpheader = c("User-Agent" = "R(2.10.0)"))
       html <- htmlTreeParse(doc, useInternalNodes = TRUE, error=function(...){})
       nodes <- getNodeSet(html, "//a[@href][@class='l']")
       return(sapply(nodes, function(x) x <- xmlAttrs(x)[[1]]))
    }

search.term <- "cran"
quotes <- "FALSE"
search.url <- getGoogleURL(search.term=search.term, quotes=quotes)

links <- getGoogleLinks(search.url)

我想找到我的搜索结果的所有链接,我得到以下结果:

> links
list()

如何获取链接? 另外我想获取google结果的标题和摘要如何获取? 最后,有没有办法获取驻留在 ChillingEffects.org 结果中的链接?

如果您查看 html 变量,您会发现搜索结果链接都嵌套在 <h3 class="r"> 标签中。

尝试将您的 getGoogleLinks 函数更改为:

getGoogleLinks <- function(google.url) {
   doc <- getURL(google.url, httpheader = c("User-Agent" = "R
                                             (2.10.0)"))
   html <- htmlTreeParse(doc, useInternalNodes = TRUE, error=function
                          (...){})
   nodes <- getNodeSet(html, "//h3[@class='r']//a")
   return(sapply(nodes, function(x) x <- xmlAttrs(x)[["href"]]))
}

我创建此函数是为了读取公司名称列表,然后获取每个公司的顶级网站结果。它会让您入门,然后您可以根据需要进行调整。

#libraries.
library(URLencode)
library(rvest)

#load data
d <-read.csv("P:\needWebsites.csv")
c <- as.character(d$Company.Name)

# Function for getting website.
getWebsite <- function(name)
{
    url = URLencode(paste0("https://www.google.com/search?q=",name))

    page <- read_html(url)

    results <- page %>% 
      html_nodes("cite") %>% # Get all notes of type cite. You can change this to grab other node types.
      html_text()

    result <- results[1]

    return(as.character(result)) # Return results if you want to see them all.
}

# Apply the function to a list of company names.
websites <- data.frame(Website = sapply(c,getWebsite))]

这里的其他解决方案对我不起作用,这是我对 @Bryce-Chamberlain 的问题的看法,它在 2019 年 8 月对我有用,它还回答了另一个封闭的问题:company name to URL in R


# install.packages("rvest")

get_first_google_link <- function(name, root = TRUE) {
  url = URLencode(paste0("https://www.google.com/search?q=",name))
  page <- xml2::read_html(url)
  # extract all links
  nodes <- rvest::html_nodes(page, "a")
  links <- rvest::html_attr(nodes,"href")
  # extract first link of the search results
  link <- links[startsWith(links, "/url?q=")][1]
  # clean it
  link <- sub("^/url\?q\=(.*?)\&sa.*$","\1", link)
  # get root if relevant
  if(root) link <- sub("^(https?://.*?/).*$", "\1", link)
  link
}

companies <- data.frame(company = c("apple acres llc","abbvie inc","apple inc"))
companies <- transform(companies, url = sapply(company,get_first_google_link))
companies
#>           company                            url
#> 1 apple acres llc https://www.appleacresllc.com/
#> 2      abbvie inc        https://www.abbvie.com/
#> 3       apple inc         https://www.apple.com/

reprex package (v0.2.1)

于 2019-08-10 创建

免费解决方案不再有效。此外,它不允许您搜索您所在地以外的地区。这是使用 Google 自定义搜索 API 的解决方案。 API 允许每天 100 次免费 API 通话。下面的函数 returns 只有 10 个结果或第 1 页。1 API 调用 returns 只有 10 个结果。

Google.Search.API <- function(keyword, google.key, google.cx, country = "us")
{
  # keyword = keywords[10]; country = "us"
  url <- paste0("https://www.googleapis.com/customsearch/v1?"
                , "key=", google.key
                , "&q=", gsub(" ", "+", keyword)
                , "&gl=", country         # Country
                , "&hl=en"                # Language from Browser, english
                , "&cx=", google.cx
                , "&fields=items(link)"
                )

  d2 <- url %>%
        httr::GET(ssl.verifypeer=TRUE) %>%
        httr::content(.) %>% .[["items"]] %>%
        data.table::rbindlist(.) %>%
        mutate(keyword, SERP = row_number(), search.engine = "Google API") %>%
        rename(source = link) %>%
        select(search.engine, keyword, SERP, source)

  pause <- round(runif(1, min = 1.1, max = 5), 1)
  if(nrow(d2) == 0)
  {cat("\nPausing", pause, "seconds. Failed for:", keyword)} else
  {cat("\nPausing", pause, "seconds. Successful for:", keyword)}

  Sys.sleep(pause)
  rm(keyword, country, pause, url, google.key, google.cx)
  return(d2)
}