LoginSignup
22

More than 5 years have passed since last update.

Go(goquery)でQiitaのスクレイピングライブラリをつくった

Last updated at Posted at 2015-10-03

high5/go-qiita-explore

スクレイピングの対象は、特定のタグを対象にした最近ストックされた投稿新着投稿になる。
※Qiitaが用意している公開APIを利用すれば同等のことができ、安定もしているので、通常、そちらの利用をおすすめします。

使用例

取得したいtagとpage(省略可能,デフォルト1)を指定して使う。

最近ストックされた投稿(例.ruby)


package main

import (
    "fmt"
    "github.com/high5/go-qiita-explore"
    "log"
)

func main() {
    explore := explore.NewExplore()
    articles, err := explore.GetStocks("Ruby")
    if err != nil {
        log.Fatal(err)
    }
    for index, article := range articles {
        if index == 0 {
            fmt.Println("------------------------------------------------------------------------------")
        }
    fmt.Printf("title:        %s \n", article.Title)
        fmt.Printf("url:          %s \n", article.URL.String())
        fmt.Printf("user:         %s \n", article.UserName)
        fmt.Printf("user_url:     %s \n", article.UserURL.String())
        fmt.Printf("stock:        %d \n", article.StockCount)
        fmt.Printf("created_time: %s \n", article.CreatedTime.Format("2006-01-02"))
        for index, tag := range article.Tags {
            tagNo := index + 1
            fmt.Printf("tag%d:         %s (http://qiita.com/tags/%s)\n", tagNo, tag, tag)
        }
        fmt.Println("------------------------------------------------------------------------------")
    }
}

新着投稿(例.JavaScript)

package main

import (
    "fmt"
    "github.com/high5/go-qiita-explore"
    "log"
)

func main() {
    explore := explore.NewExplore()
    articles, err := explore.GetItems("JavaScript", 1)
    if err != nil {
        log.Fatal(err)
    }
    for index, article := range articles {
        if index == 0 {
            fmt.Println("------------------------------------------------------------------------------")
        }
    fmt.Printf("title:        %s \n", article.Title)
        fmt.Printf("url:          %s \n", article.URL.String())
        fmt.Printf("user:         %s \n", article.UserName)
        fmt.Printf("user_url:     %s \n", article.UserURL.String())
        fmt.Printf("stock:        %d \n", article.StockCount)
        fmt.Printf("created_time: %s \n", article.CreatedTime.Format("2006-01-02"))
        for index, tag := range article.Tags {
            tagNo := index + 1
            fmt.Printf("tag%d:         %s (http://qiita.com/tags/%s)\n", tagNo, tag, tag)
        }
        fmt.Println("------------------------------------------------------------------------------")
    }
}

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
22