user2355903
user2355903

Reputation: 643

Using R to perform a web query and return URL's

I am fairly new to R, and have never used it for writing a web query before. I was wondering if there were any preexisting packages out there that would fit my needs. I am trying to search for a company and return the URL of their website. I have the company name, address, and phone number. Is there a way to run a program that will check the website against the information I have to confirm it's the correct website?

Upvotes: 1

Views: 2354

Answers (2)

hadley
hadley

Reputation: 103928

Thomas' function is a little easier to write with httr because it:

  • automatically manages handles for you

  • automatically follows redirects

  • returns an object that represents the results of the request

And here's the function:

library(httr)
geturlname <- function(name){
  url <- paste0("http://google.com/search?btnI=1&q=", name)
  GET(url)$url
}
geturlname("Apple")
geturlname("Google")
geturlname("Blockbuster")

Upvotes: 4

Thomas
Thomas

Reputation: 44555

Can't guarantee that this will work every time but definitely use the RCurl package

library(RCurl)
geturlname <- function(name){
    h = getCurlHandle()
    z <- getURL(paste0("http://google.com/search?btnI=1&q=",name), # google i'm feeling lucky
        followlocation=TRUE, curl=h)
    getCurlInfo(h)$effective.url # catch the url redirect
}
geturlname("Apple")
geturlname("Google")
geturlname("Blockbuster")

Upvotes: 5

Related Questions