Golang福利爬蟲
沒事的時候跑兩把,窮人專用。
package main
import (
"bytes"
"fmt"
"github.com/PuerkitoBio/goquery"
"log"
"math"
"net/http"
"runtime"
"strconv"
"strings"
"sync"
"time"
)
func Scraper(page string, now int64) string {
// Request the HTML page.
ScrapeURL := "https://51.ruyo.net/page/" + page
client := &http.Client{}
reqest, _ := http.NewRequest("GET", ScrapeURL, nil)
reqest.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
reqest.Header.Set("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3")
//reqest.Header.Set("Accept-Encoding", "gzip,deflate,sdch")
reqest.Header.Set("Accept-Language", "zh-CN,zh;q=0.8")
reqest.Header.Set("Cache-Control", "max-age=0")
reqest.Header.Set("Connection", "keep-alive")
reqest.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36")
res, err := client.Do(reqest)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
}
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Fatal(err)
}
// Find the review items
var buffer bytes.Buffer
buffer.WriteString("**********Scraped page " + page + "**********\n")
doc.Find(".panel-body .entry-header").Each(func(i int, s *goquery.Selection) {
// For each item found, get the band and title
title := s.Find("h3").ChildrenFiltered("a").Text()
url, _ := s.Find("h3").ChildrenFiltered("a").Attr("href")
etime, _ := s.Find("div").ChildrenFiltered("time").Attr("datetime")
etime = strings.Replace(etime, "+00:00", "", -1) + "Z"
dt, _ := time.Parse(time.RFC3339, etime)
intdt := dt.Unix()
difftime := math.Abs(float64(now - intdt))
//fmt.Println(now, intdt, difftime)
if difftime <= 2592000 {
buffer.WriteString("Review " + strconv.Itoa(i) + ": " + title + "\n" + url + "\n" + etime + "\n")
}
})
return buffer.String()
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
now := time.Now().Unix()
ch := make(chan string, 5)
wg := &sync.WaitGroup{}
var page string
for i := 1; i < 6; i++ {
wg.Add(1)
go func(i int) {
page = strconv.Itoa(i)
fmt.Printf("Scraping page %s...\n", page)
ch <- Scraper(page, now)
wg.Done()
}(i)
}
wg.Wait()
//print result
for i := 0; i < 6; i++ {
fmt.Println(<-ch)
}
}
相關文章
- 3 行寫爬蟲 - 使用 Goribot 快速構建 Golang 爬蟲爬蟲Golang
- Golang爬蟲+正規表示式Golang爬蟲
- 爬蟲福利----妹子圖網MM批量下載爬蟲
- Golang 網路爬蟲框架gocolly/collyGolang爬蟲框架
- 『No20: Golang 爬蟲上手指南』Golang爬蟲
- 用Golang寫爬蟲(六) - 使用collyGolang爬蟲
- golang-spider-從單任務版爬蟲到併發爬蟲01GolangIDE爬蟲
- 使用 Golang 寫爬蟲經驗總結Golang爬蟲
- 【Python學習】爬蟲爬蟲爬蟲爬蟲~Python爬蟲
- 爬蟲福利二 之 妹子圖網MM批量下載爬蟲
- [烈格黑街][福利]第一個java爬蟲程式Java爬蟲
- Golang爬蟲,Go&&正則爬取資料,槓桿的Golang爬蟲
- Golang框架beego電影網爬蟲小試牛刀Golang框架爬蟲
- 一個基於 golang 的爬蟲電影站Golang爬蟲
- 深入淺出爬蟲之道: Python、Golang與GraphQuery的爬蟲PythonGolang
- 爬蟲:多程式爬蟲爬蟲
- python爬蟲---網頁爬蟲,圖片爬蟲,文章爬蟲,Python爬蟲爬取新聞網站新聞Python爬蟲網頁網站
- 通用爬蟲與聚焦爬蟲爬蟲
- 爬蟲--Scrapy簡易爬蟲爬蟲
- 爬蟲進階:反反爬蟲技巧爬蟲
- 反爬蟲之字型反爬蟲爬蟲
- 爬蟲爬蟲
- 【爬蟲】爬蟲專案推薦 / 思路爬蟲
- 網路爬蟲——爬蟲實戰(一)爬蟲
- 爬蟲帶你瞭解一下Golang的市場行情爬蟲Golang
- Golang爬蟲實踐-將你的掘金小冊裝進kindleGolang爬蟲
- 爬蟲界的福利--touchRobot,機器模擬觸碰滑動(已開源)爬蟲
- 2個月精通Python爬蟲——3大爬蟲框架+6場實戰+反爬蟲技巧+分散式爬蟲Python爬蟲框架分散式
- Python爬蟲教程-01-爬蟲介紹Python爬蟲
- Java爬蟲與Python爬蟲的區別?Java爬蟲Python
- python就是爬蟲嗎-python就是爬蟲嗎Python爬蟲
- 爬蟲與反爬蟲技術簡介爬蟲
- request爬蟲爬蟲
- nodejs 爬蟲NodeJS爬蟲
- 科普:爬蟲爬蟲
- python 爬蟲Python爬蟲
- app爬蟲APP爬蟲
- 爬蟲案例爬蟲