user656925
user656925

Reputation:

Google API | URL to site title

For example to get the favicon of a site I can use http://www.google.com/s2/favicons?domain= and fill in the domain. Google returns the favicon. I would also like to pull the title.

I know that I could parse the title from the html on the server side...or I could use javascript document.title on the client side. But I don't want to have to download the whole site.

I used the favicon example b.c. it was a good example of how you have data about a site available on the web with out having to do any "heavy lifting"

There must be a similar for the title. Essentially I want to match a URL to title.

Upvotes: 10

Views: 5105

Answers (4)

Chris Hayes
Chris Hayes

Reputation: 13716

With Google Search API

  1. Create an API key here: https://developers.google.com/custom-search/v1/overview#api_key
  2. Create a "Programmable Search Engine" from here: https://programmablesearchengine.google.com/ You can restrict it to a specific domain in these settings if desired.
  3. Run a GET request with this URL: https://www.googleapis.com/customsearch/v1?key=${searchAPIKey}&cx=${searchID}&q=${url}
  • searchAPIKey comes from step 1
  • searchID comes from step 2
  • url is the search text, putting a url will usually put that result first in the results. However, newer or hidden links won't show up in these results.

In the JSON response, you can get the title of the first result with items[0].title

Javascript Fetch Example with Async/Await

const searchAPIKey = ''
const searchID = ''

fetch(`https://www.googleapis.com/customsearch/v1?key=${searchAPIKey}&cx=${searchID}&q=${url}`).then(function(response) {
  return response.json();
}).then(function(data) {
  console.log('title:', data.items[0].title)
}

Upvotes: 0

472084
472084

Reputation: 17895

This post has a very nice piece of code which fetches the URL, description and keywords...

Getting title and meta tags from external website

You do have to download the whole pages source, but its only one page and using the PHP DOMDocument class is very efficient.

You don't have to load the whole page to get a favicon because its a separate file but titles are stored inside the page source.

Upvotes: 1

Overv
Overv

Reputation: 8529

You can make use of the Google custom search API to get the title of a website. Just search for "info:siteurl" and grab the title of the first request. I don't know exactly what you want to do, but it allows for 100 requests a day.

See details of the API here: http://code.google.com/apis/customsearch/v1/reference.html

Upvotes: 2

Tny
Tny

Reputation: 165

http://forums.digitalpoint.com/showthread.php?t=605681

I think you are looking for something like this

Upvotes: 0

Related Questions