go爬虫:抓取股票实时股份信息发送到邮箱

u011466175 · · 6682 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

买股票的时候因为股份时时变动,所以卖出时机不好吃了好多亏,所以工作之余就想自己弄一个实时的接收自己想要的股份信息。

发送邮件用的smtp服务包,不多说了,直接上代码。

go_library项目的send_mail.go代码:

package gemail

import (
	"log"
	"net/smtp"
	"strings"
)

//smtp服务发送邮件, mailType表示邮件格式是普通文件还是html或其他
func SendToMail(host, user, password, to, mailType, subject, body string) {
	hp := strings.Split(host, ":")
	auth := smtp.PlainAuth("", user, password, hp[0])
	content_type := "Content-Type: text/" + mailType + "; charset=UTF-8"
	msg := []byte("To: " + to + "\r\nFrom: " + user + "\r\nSubject: " + subject + "\r\n" + content_type + "\r\n\r\n" + body)
	send_to := strings.Split(to, ";")
	err := smtp.SendMail(host, auth, user, send_to, msg)
	if err != nil {
		log.Println("SendMail error: ", err)
	}
}


获取股票信息的项目:

项目结构:


crawl_foundation.go代码:

package crawl

import (
	"go_library/gconvert"
	"go_library/ghttp"
	"log"
	"math"
	"strings"
	"time"
)

func ParseStr(hqStr string) []string {
	baseData := strings.Split(hqStr, "\"")
	base := strings.Split(baseData[1], ",")
	return base
}

func Fetch_shsz(url string) string {
	client := ghttp.HttpClient(time.Second * 10)
	if client == nil {
		log.Println("get http client error")
		return ""
	}
	data := ghttp.HttpGet(client, url, "")
	base := ParseStr(data)
	str := gconvert.ConvertCharacterEncoding(base[0], "gb2312", "utf-8") + "\n"
	str += " 当前最新价:" + base[3] + " | 当前涨跌率:"
	agoPrice := gconvert.StringToFloat64(base[2])
	nowPrice := gconvert.StringToFloat64(base[3])
	lv := ""
	if agoPrice > nowPrice {
		lv = "-"
	}
	upDown := math.Abs(nowPrice-agoPrice) / agoPrice * 100
	str += lv + gconvert.Float64ToString(upDown, 2) + "%\n"
	str += " 今日最高价:" + base[4] + " | 今日最低价:" + base[5] + "\n"
	str += " 今日开盘价:" + base[1] + " | 昨日收盘价:" + base[2] + "\n\n"
	return str
}

func Fetch_CompositeIndex(url string) string {
	client := ghttp.HttpClient(time.Second * 10)
	if client == nil {
		log.Println("get http client error")
		return ""
	}
	data := ghttp.HttpGet(client, url, "")
	base := ParseStr(data)
	str := gconvert.ConvertCharacterEncoding(base[0], "gb2312", "utf-8") + " 当前指数:" + base[1] + " | 涨跌额:" + base[2] + " | 涨跌率:" + base[3] + "%\n\n"
	return str
}

main.go代码:

package main

import (
	"go_library/gemail"
	"go_library/gtime"
	"stock/crawl"
	"time"
)

type User struct {
	user     string
	password string
	host     string
	to       string
	mailType string
	subject  string
	body     string
}

func NewUser() *User {
	return &User{
		user:     "xxx@qq.com", //自己的邮箱
		password: "xxx", //这个密码是smtp服务的密码,在邮箱中打开pop3/smtp服务,设立独立密码即可
		host:     "smtp.qq.com:587",
		to:       "xxx@qq.com", //要发送到哪个邮箱
	}
}

type CodeLink struct {
	grail       []string
	singleStock []string
}

func NewCodeLink() *CodeLink {
	task := &CodeLink{}
	task.grail = []string{
		"http://hq.sinajs.cn/list=s_sh000001",
		"http://hq.sinajs.cn/list=s_sz399001",
		"http://hq.sinajs.cn/list=s_sz399006",
	}
	task.singleStock = []string{
		"http://hq.sinajs.cn/list=sz150304",
		"http://hq.sinajs.cn/list=sz000582",
		"http://hq.sinajs.cn/list=sz150153",
		"http://hq.sinajs.cn/list=sz150224",
		"http://hq.sinajs.cn/list=sz150282",
		"http://hq.sinajs.cn/list=sz150201",
		"http://hq.sinajs.cn/list=sz150224",
		"http://hq.sinajs.cn/list=sz150236",
		"http://hq.sinajs.cn/list=sh502012",
		"http://hq.sinajs.cn/list=sh502055",
	}
	return task
}

func (c *CodeLink) Task() {
	u := NewUser()
	for _, grail := range c.grail {
		u.body += crawl.Fetch_CompositeIndex(grail)
	}
	for _, singleStock := range c.singleStock {
		u.body += crawl.Fetch_shsz(singleStock)
	}
	u.subject = "股票当前情况"
	u.mailType = "plain"
	gemail.SendToMail(u.host, u.user, u.password, u.to, u.mailType, u.subject, u.body)
}

func (c *CodeLink) IsStockTime() bool {
	if !gtime.IsWorkDay() {
		return false
	}
	now := time.Now()
	minute := now.Hour()*60 + now.Minute()
	if (minute >= 570 && minute <= 690) || (minute >= 780 && minute <= 900) {
		return true
	}
	return false
}

func main() {
	codeLink := NewCodeLink()
	ticker := time.NewTicker(time.Minute * 10)
	for _ = range ticker.C {
		if codeLink.IsStockTime() {
			codeLink.Task()
		}
	}
}





注:有什么编码、爬虫等私活可以联系:cugbliwei@gmail.com代做







有疑问加站长微信联系(非本文作者)

本文来自:CSDN博客

感谢作者:u011466175

查看原文:go爬虫:抓取股票实时股份信息发送到邮箱

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

6682 次点击  ∙  1 赞  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传