没事的时候跑两把,穷人专用。

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)
}
}

Golang福利爬虫的更多相关文章

  1. Golang分布式爬虫:抓取煎蛋文章|Redis/Mysql|56,961 篇文章

    --- layout: post title: "Golang分布式爬虫:抓取煎蛋文章" date: 2017-04-15 author: hunterhug categories ...

  2. Golang 网络爬虫框架gocolly/colly 四

    Golang 网络爬虫框架gocolly/colly 四 爬虫靠演技,表演得越像浏览器,抓取数据越容易,这是我多年爬虫经验的感悟.回顾下个人的爬虫经历,共分三个阶段:第一阶段,09年左右开始接触爬虫, ...

  3. Golang 网络爬虫框架gocolly/colly 三

    Golang 网络爬虫框架gocolly/colly 三 熟悉了<Golang 网络爬虫框架gocolly/colly一>和<Golang 网络爬虫框架gocolly/colly二& ...

  4. Golang 网络爬虫框架gocolly/colly 二 jQuery selector

    Golang 网络爬虫框架gocolly/colly 二 jQuery selector colly框架依赖goquery库,goquery将jQuery的语法和特性引入到了go语言中.如果要灵活自如 ...

  5. Golang 网络爬虫框架gocolly/colly 一

    Golang 网络爬虫框架gocolly/colly 一 gocolly是用go实现的网络爬虫框架,目前在github上具有3400+星,名列go版爬虫程序榜首.gocolly快速优雅,在单核上每秒可 ...

  6. Golang 网络爬虫框架gocolly/colly 五 获取动态数据

    Golang 网络爬虫框架gocolly/colly 五 获取动态数据 gcocolly+goquery可以非常好地抓取HTML页面中的数据,但碰到页面是由Javascript动态生成时,用goque ...

  7. Golang 简单爬虫实现,爬取小说

    为什么要使用Go写爬虫呢? 对于我而言,这仅仅是练习Golang的一种方式. 所以,我没有使用爬虫框架,虽然其很高效. 为什么我要写这篇文章? 将我在写爬虫时找到资料做一个总结,希望对于想使用Gola ...

  8. 基于golang分布式爬虫系统的架构体系v1.0

    基于golang分布式爬虫系统的架构体系v1.0 一.什么是分布式系统 分布式系统是一个硬件或软件组件分布在不同的网络计算机上,彼此之间仅仅通过消息传递进行通信和协调的系统.简单来说就是一群独立计算机 ...

  9. 试验一下Golang 网络爬虫框架gocolly/colly

    参考:http://www.cnblogs.com/majianguo/p/8186429.html 框架源码在 github.com/gocolly/colly 代码如下(github源码中的dem ...

随机推荐

  1. 在react+redux+axios项目中使用async/await

    Async/Await Async/Await是尚未正式公布的ES7标准新特性.简而言之,就是让你以同步方法的思维编写异步代码.对于前端,异步任务代码的编写经历了 callback 到现在流行的 Pr ...

  2. 重写nyoj2——括号匹配

    #include "bits/stdc++.h" using namespace std; int comp(char s1,char s2){ ; ; } int main() ...

  3. OCP-1Z0-051-V9.02-13题 单引号的使用

    13. View the Exhibit and examine the structure of the PRODUCTS table. You need to generate a report ...

  4. SpringBoot利用注解@Value获取properties属性为null

    参考:https://www.cnblogs.com/zacky31/p/8609990.html 今天在项目中想使用@Value来获取Springboot中properties中属性值. 场景:定义 ...

  5. 在springboot中验证表单信息(六)

    构建工程 创建一个springboot工程,由于用到了 web .thymeleaf.validator.el,引入相应的起步依赖和依赖,代码清单如下: 1 2 3 4 5 6 7 8 9 10 11 ...

  6. from…import 语句

  7. 快捷键设置 keyiing.json

    // 快捷键设置 keyiing.json // 将键绑定放入此文件中以覆盖默认值 [     /* // 转换大写     {         "key" : "ctr ...

  8. composer install Your requirements could not be resolved to an installable set of packages

    composer install --ignore-platform-reqs 或者 composer update --ignore-platform-reqs

  9. MAVEN 创建 JAR项目

    从Maven模板创建一个项目 在终端或命令提示(windows)中,浏览到要创建JAVA项目的文件夹下 键入如下命令: mvn archetype:generate -DgroupId={projec ...

  10. Win10系列:VC++ Direct3D模板介绍1

    Visual Studio为开发Direct3D应用程序提供了便捷的模版,读者可以不必手动去新建Direct3D中所使用到的基础资源,而只需专注于图形的绘制.本小节主要为读者介绍这个模版中用于绘制图形 ...