Golang 爬取每日早报并推送到微信
本帖最后由 52loli 于 2022-7-11 08:20 编辑### 使用教程
* 打开(https://wxpusher.zjiecode.com/docs/#/?id=%e5%bf%ab%e9%80%9f%e6%8e%a5%e5%85%a5)官网,微信扫码登录,根据官方文档进行操作
!(https://dd-static.jd.com/ddimg/jfs/t1/151082/18/25118/9163/62c707b7E927d4e2d/0f24e4fe5bfb358b.png)
!(https://dd-static.jd.com/ddimg/jfs/t1/114523/19/27704/21679/62c707b7E2329389e/5c4a6d3c9ef81442.png)
* 私发复制UID
!(https://dd-static.jd.com/ddimg/jfs/t1/113077/5/28055/33060/62c707b8Efcafaf9a/4e54f36abea00692.png)
* 群发复制Topicid
!(https://dd-static.jd.com/ddimg/jfs/t1/207196/17/24433/32879/62c707b7Efc5a1044/efec18a41659af34.png)
* 请先运行一次软件,然后会生成一个config.json的文件,然后在文件中填入必要的参数
### Github
* (https://github.com/smloli/todayNewsSpider)
### 预览
!(https://dd-static.jd.com/ddimg/jfs/t1/89585/25/30048/611463/62cb6c6aE5bd16761/951d292bdcd40ea7.jpg)
```Go
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"loli/wxPusher"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/antchfx/htmlquery"
)
type Config struct {
// 密钥
AppToken string `json:"appToken"`
// 主题id 为空不转发
TopicIds []int `json:"topicIds"`
// 个人id 为空不转发
Uids []string `json:"uids"`
// 状态码
Data []mapinterface{}
}
type Image struct {
ImageContent struct {
Image struct {
Url string
}
}
}
type BingWallpaper struct {
MediaContents []Image
}
// 获取Bing当日壁纸
func getBingWallpaper() string {
var bing BingWallpaper
url := "https://cn.bing.com/hp/api/model"
resp := get(url, nil)
json.Unmarshal(*resp, &bing)
return bing.MediaContents.ImageContent.Image.Url
}
func get(url string, headers *mapstring) *[]byte {
client := &http.Client{}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Cache-Control", "no-cache")
req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36")
if headers != nil {
for k, v := range *headers {
req.Header.Set(k, v)
}
}
resp, err := client.Do(req)
if err != nil {
fmt.Println("网络连接超时!", err)
return nil
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return &body
}
func parse() {
url := "https://www.163.com/dy/media/T1603594732083.html"
resp := get(url, nil)
if resp == nil {
return
}
html, _ := htmlquery.Parse(bytes.NewReader(*resp))
root := htmlquery.Find(html, `//li[@class="js-item item"]/a/@href`)
todayUrl := htmlquery.InnerText(root)
headers := mapstring{
"referer": url,
}
resp = get(todayUrl, &headers)
if resp == nil {
return
}
html, _ = htmlquery.Parse(bytes.NewReader(*resp))
root = htmlquery.Find(html, `//div[@class="post_body"]/p/text()`)
var con string
for i, v := range root {
if i == 0 {
con += "![](" + getBingWallpaper() + ")\n\n**<center>" + htmlquery.InnerText(v) + "</center>**\n\n"
continue
}
if i == len(root)-1 {
con += "\n> " + htmlquery.InnerText(v)
break
}
con += strings.Replace(htmlquery.InnerText(v), "、", ". ", 1) + "\n"
}
push(con, "今日早报")
}
func push(content string, summary string) {
var loli wxPusher.Loli
var con Config
readConfig(&con)
if con.AppToken == "" || (con.TopicIds == 0 && con.Uids == "") {
fmt.Println("config.json配置错误!")
return
}
if con.TopicIds == 0 {
con.TopicIds = nil
}
if con.Uids == "" {
con.Uids = nil
}
res := loli.Send(con.AppToken, content, summary, 3, con.TopicIds, con.Uids)
json.Unmarshal(*res, &con)
fmt.Println(con.Data["code"], con.Data["status"])
}
func readConfig(con *Config) {
path := filepath.Dir(os.Args) + filepath.FromSlash("/") + "config.json"
if _, err := os.Stat(path); err != nil {
f, err := os.Create(path)
if err != nil {
fmt.Println("config.json创建失败!", err)
time.Sleep(3 * time.Second)
os.Exit(0)
}
defer f.Close()
con.TopicIds = make([]int, 1)
con.Uids = make([]string, 1)
c, _ := json.MarshalIndent(con, "", " ")
f.Write(c)
fmt.Println("请在config.json中填入必要的配置!")
time.Sleep(3 * time.Second)
os.Exit(0)
}
f, err := os.Open(path)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
fi, _ := f.Stat()
data := make([]byte, fi.Size())
f.Read(data)
json.Unmarshal(data, &con)
}
func main() {
parse()
}
``` songing 发表于 2022-7-8 09:28
不能每天自动发送吗?
可以放到服务器上设定每天早上7点自动运行一次,嫌麻烦的也可以微信打开这个链接wxpusher.zjiecode.com/wxuser/?type=2&id=6574#/follow关注主题后,每天早上7点群发早报 LeonSmith153 发表于 2023-11-16 14:33
楼主,想问下我这边运行`go run main.go`后总是提示“请在config.json中填入必要的配置!”
我使用了如下 ...
这个要编译后再运行,不然每次生成的config.json都会在不同的临时目录
```
{
"appToken": "",
"topicIds": [
0,
1,
2
],
"uids": [
"0",
"1",
"2"
]
}
``` 可以,用用试试看 技术贴,学习了。 可以,挺有意思的!支持! 111111111 技术贴,学习了,支持楼主:lol 每日新闻确实是一个获取知识的途径 学习一下逻辑 技术贴,学习了感谢楼主教学 新手学习!