LoginSignup
0
0

More than 5 years have passed since last update.

Golangで画像をスクレイプする

Last updated at Posted at 2018-07-14

Agoutiを使って操作する
webdriverはchromedriverを使用

対応ブラウザー
- Google
- yahoo
- Bing

以下をインストール

$ brew cask install chromedriver
$ go get github.com/PuerkitoBio/goquery
$ go get github.com/sclevine/agouti
$ go get gitlab.com/torufukui/octopus_stock

octopus_stockはGoogle、Yahoo、Bingから画像を取得できるように作った簡単なライブラリ
以下のようにして使用する


package main

  import (
    "gitlab.com/torufukui/octopus_stock"
    "gitlab.com/torufukui/octopus_stock/google"
    "gitlab.com/torufukui/octopus_stock/yahoo"
    "gitlab.com/torufukui/octopus_stock/bing"
  )

  func main() {
    stock_image := &octopus_stock.StockImage{
      FolderName: "Folder name",  // Specify the folder to download
      KeyWord:    "keyword name", // Specify search keywords
    }

    google := google.New(stock_image)
    google.ImageStock()

    yahoo := yahoo.New(stock_image)
    yahoo.ImageStock()

    bing := bing.New(stock_image)
    bing.ImageStock()
  }

コード、仕組みの解説

FolderNameは保存するフォルダ名を指定する、コードを実行したカレントパス上にフォルダが作成されダウンロードした画像が保存される

KeyWordはダウンロードしたいキーワード名を指定する

ダウンロードしてきたファイル名はURLをSHA1に変換して16進数でつけているがこれはURLからファイルの重複を防ぐため

デフォルトではchromedriverはheadlessモードに指定しているがブラウザを表示させたかったら内部のコードをいじってheadlessモードを外して対応してください

うまくいけば、一キーワードにつき1000枚の画像のダウンロードができます

画像のスクレイプはいたちごっこなのでブラウザ側の仕様変更で取得できなくなる可能性があります

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0