Google Custom Search APIを叩いてみた

まえがき

ふーむ、と思ってちょっと調べてみました。
Googleのことだから多分検索用のAPI用意されてるだろうし、そこにデータ入ってないかな?がスタートです。

Google Custom Search APIについて

調べてみたら、Google Custom Search JSON APIというものがあるみたいです。

qiita.com

手順については上記ブログで記載があるので割愛します。
一箇所だけ、get_search_response.py内の

def getSearchResponse(project_id, keyword_id, keyword):

は呼び出し元がtarget_keywordだけしか使って使っていなさそうだったので、それ以外の引数を消しました。

def getSearchResponse(keyword):

取れたデータについて

target_keywordを自分のブログのURLを指定してみました。

    target_keyword = 'https://www.st1t.com/entry/2018/12/27/174532'
$ jq . data/response/response_20181228.json 
{
  "snapshot_ymd": "20181228",
  "snapshot_timestamp": "2018/12/28 20:34:11",
  "response": [
    {
      "kind": "customsearch#search",
      "url": {
        "type": "application/json",
        "template": "https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json"
      },
      "queries": {
        "request": [
          {
            "title": "Google Custom Search - https://www.st1t.com/entry/2018/12/27/174532",
            "totalResults": "1",
            "searchTerms": "https://www.st1t.com/entry/2018/12/27/174532",
            "count": 1,
            "startIndex": 1,
            "language": "lang_ja",
            "inputEncoding": "utf8",
            "outputEncoding": "utf8",
            "safe": "off",
            "cx": "000541688366129963034:bisdpfb8q-4"
          }
        ]
      },
      "context": {
        "title": "カスタム検索エンジン"
      },
      "searchInformation": {
        "searchTime": 0.142453,
        "formattedSearchTime": "0.14",
        "totalResults": "1",
        "formattedTotalResults": "1"
      },
      "items": [
        {
          "kind": "customsearch#result",
          "title": "検索システムを作る上で役立つ本 - infra.log",
          "htmlTitle": "検索システムを作る上で役立つ本 - infra.log",
          "link": "https://www.st1t.com/entry/2018/12/27/174532",
          "displayLink": "www.st1t.com",
          "snippet": "1 日前 ... ... 初めて見たくらい良書だと思いました。 買うのが不安な方は立ち読みだけでもいい\nのでまずは見てもらうといいのかなーと思います。 https://www.kindaikagaku.co.jp/\nimg/book/ · https://www.kindaikagaku.co.jp/information/kd0577.htm ...",
          "htmlSnippet": "1 日前 <b>...</b> ... 初めて見たくらい良書だと思いました。 買うのが不安な方は立ち読みだけでもいい<br>\nのでまずは見てもらうといいのかなーと思います。 <b>https</b>://www.kindaikagaku.co.jp/<br>\nimg/book/ &middot; <b>https</b>://www.kindaikagaku.co.jp/information/kd0577.htm&nbsp;...",
          "cacheId": "61D4dXdaVo0J",
          "formattedUrl": "https://www.st1t.com/entry/2018/12/27/174532",
          "htmlFormattedUrl": "<b>https</b>://www.<b>st1t.com/entry</b>/<b>2018/12/27/174532</b>",
          "pagemap": {
            "cse_thumbnail": [
              {
                "width": "170",
                "height": "216",
                "src": "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcShrcLWXuO7jeYzIprdKhrVSMarpKuAKRZZyzQUgBSu4_Dm_JVH-wAukx8"
              }
            ],
            "metatags": [
              {
                "viewport": "width=device-width, initial-scale=1.0",
                "og:title": "検索システムを作る上で役立つ本 - infra.log",
                "og:type": "article",
                "og:url": "https://www.st1t.com/entry/2018/12/27/174532",
                "og:image": "https://www.kindaikagaku.co.jp/img/book/KD0577-170.jpg",
                "og:description": "まえがき 最近Elasticsearchを触る機会が増えつつあるのですが、 そもそも検索を行う際に何をどうしたら良いのか全くわからんって時のためおすすめの書籍をいくつか紹介したいと思います。 また、達人出版社さんから冬休み合同フェアというのが開催されており、 こちらだと電子書籍が半額で買えるのでオススメです。 tatsu-zine.com 初級編 Elasticsearch実践ガイド book.impress.co.jp 全文検索を行おうと思ったら大体最近の流れはElasticsearchにいきつくパターンが多いかと思います。 ただ、公式のドキュメントは英語だったり、そもそも全文検索の界隈で当…",
                "og:site_name": "infra.log",
                "article:published_time": "1545900332",
                "twitter:card": "summary_large_image",
                "twitter:image": "https://www.kindaikagaku.co.jp/img/book/KD0577-170.jpg",
                "twitter:title": "検索システムを作る上で役立つ本 - infra.log",
                "twitter:description": "まえがき 最近Elasticsearchを触る機会が増えつつあるのですが、 そもそも検索を行う際に何をどうしたら良いのか全くわからんって時のためおすすめの書籍をいくつか紹介したいと思います。 また、達人出版社さんから冬休み合同フェアというのが開催されており、 こちらだと電子書籍が半額で買えるのでオススメです。 tats…",
                "twitter:app:name:iphone": "はてなブログアプリ",
                "twitter:app:id:iphone": "583299321",
                "twitter:app:url:iphone": "hatenablog:///open?uri=https%3A%2F%2Fwww.st1t.com%2Fentry%2F2018%2F12%2F27%2F174532",
                "twitter:site": "@st_1t"
              }
            ],
            "article": [
              {
                "name": "検索システムを作る上で役立つ本 - infra.log",
                "image": "https://www.kindaikagaku.co.jp/img/book/KD0577-170.jpg"
              }
            ],
            "cse_image": [
              {
                "src": "https://www.kindaikagaku.co.jp/img/book/KD0577-170.jpg"
              }
            ]
          }
        }
      ]
    }
  ]
}
$ 

jqで日付とタイトルを抜き出す

article:published_timeはUNIXタイムなので人が見るときは変換したほうが良いかも。

$ jq '.response[].items[].pagemap.metatags[]["og:title","article:published_time"]' data/response/response_20181228.json 
"検索システムを作る上で役立つ本 - infra.log"
"1545900332"

あとがき

というわけでさくっとやってみたのですが、まだ課題があります。
SEOは門外漢すぎて知らなかったのですが、
このarticle:published_timeはOpen Graph Protocol(OGP)というSEO対策のためにホームページ作成者がよしなにいれこむものみたいでした。
(なるほど、ブログにリンク貼ると以下のように画像と説明がでるのもOGPのおかげだったんですね。。。)

www.asahi-net.or.jp

なので、ものによってはやはりmetaタグがなかったり、article:published_time自体がなかったりしました。
Googleが初めてIndexしたタイミングの日時が取得できることを密かに期待していたのですが、残念💧