| 1 | package src |
| 2 | |
| 3 | import ( |
| 4 | "log" |
| 5 | "net/http" |
| 6 | "time" |
| 7 | "net" |
| 8 | "fmt" |
| 9 | |
| 10 | "github.com/gocolly/colly" |
| 11 | ) |
| 12 | |
| 13 | func Scrape (gurl string) []Route { |
| 14 | ua := "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " + |
| 15 | "AppleWebKit/537.36 (KHTML, like Gecko) " + |
| 16 | "Chrome/110.0.0.0 Safari/537.36" |
| 17 | |
| 18 | sc := colly.NewCollector( |
| 19 | colly.AllowURLRevisit(), |
| 20 | colly.Async(true), |
| 21 | ) |
| 22 | |
| 23 | sc.WithTransport(&http.Transport { |
| 24 | Proxy: http.ProxyFromEnvironment, |
| 25 | DialContext: (&net.Dialer{ |
| 26 | Timeout: 30 * time.Second, |
| 27 | KeepAlive: 30 * time.Second, |
| 28 | DualStack: true, |
| 29 | }).DialContext, |
| 30 | ForceAttemptHTTP2: true, |
| 31 | MaxIdleConns: 100, |
| 32 | IdleConnTimeout: 90 * time.Second, |
| 33 | TLSHandshakeTimeout: 10 * time.Second, |
| 34 | ExpectContinueTimeout: 1 * time.Second, |
| 35 | }) |
| 36 | |
| 37 | sc.OnRequest(func(r *colly.Request) { |
| 38 | r.Headers.Set("User-Agent", ua) |
| 39 | r.Headers.Set( |
| 40 | "Accept", |
| 41 | "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", |
| 42 | ) |
| 43 | r.Headers.Set("Accept-Language", "en-US,en;q=0.5") |
| 44 | }) |
| 45 | |
| 46 | sc.OnError(func(_ *colly.Response, err error) { |
| 47 | log.Fatal("エラー:", err) |
| 48 | }) |
| 49 | |
| 50 | var routeArr []Route |
| 51 | |
| 52 | for i := 1; i <= 3; i++ { |
| 53 | route := fmt.Sprintf("div#route%02d", i) |
| 54 | sc.OnHTML("div.elmRouteDetail " + route, func (e *colly.HTMLElement) { |
| 55 | Routes := getRouteDetail(e) |
| 56 | routeArr = append(routeArr, Routes) |
| 57 | }) |
| 58 | } |
| 59 | |
| 60 | sc.Visit(gurl) |
| 61 | sc.Wait() |
| 62 | |
| 63 | return routeArr |
| 64 | } |