Last active
November 22, 2016 18:02
-
-
Save 178inaba/437f1137458a9a1da597b1db8ff76a18 to your computer and use it in GitHub Desktop.
Web site title getter. Using goquery.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
"net/http" | |
"time" | |
"github.com/PuerkitoBio/goquery" | |
log "github.com/Sirupsen/logrus" | |
) | |
var ( | |
c = new(http.Client) | |
) | |
func init() { | |
c.CheckRedirect = func(req *http.Request, via []*http.Request) error { return errors.New("not redirect") } | |
} | |
func main() { | |
for id := 28090; id > 0; id-- { | |
getTitle(id) | |
time.Sleep(time.Second / 2) | |
} | |
} | |
func getTitle(id int) { | |
resp, err := c.Get(fmt.Sprintf("http://news.ibc.co.jp/item_%d.html", id)) | |
if err != nil { | |
log.Error(err) | |
return | |
} | |
defer resp.Body.Close() | |
if resp.StatusCode == 200 { | |
doc, err := goquery.NewDocumentFromResponse(resp) | |
if err != nil { | |
log.Error(err) | |
return | |
} | |
title := doc.Find("title").Text() | |
log.Infof("OK! %d, Title: %s", id, title) | |
} else { | |
log.Errorf("NG! %d", id) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment