1.基于幽灵蛛pholcus开源项目的规则

直接贴代码,代码可以更改后用于其他爬虫项目

package pholcus_lib

// 基础包
import (
// "github.com/henrylee2cn/pholcus/common/goquery" //DOM解析
"github.com/henrylee2cn/pholcus/app/downloader/request" //必需
. "github.com/henrylee2cn/pholcus/app/spider" //必需
// . "github.com/henrylee2cn/pholcus/app/spider/common" //选用
// "github.com/henrylee2cn/pholcus/logs"
// net包
// "net/http" //设置http.Header
// "net/url"
// 编码包
// "encoding/xml"
//"encoding/json"
// 字符串处理包
//"regexp"
// "strconv"
// "fmt"
// "math" //"net/http"
"strconv"
"regexp"
"fmt"
"encoding/json"
"net/url"
//"strings"
//"strings"
"strings"
"github.com/henrylee2cn/pholcus/common/goquery"
//"net/http"
)
////获取unix时间
var millisecond int64
//用户名
var name string
//密码
var password string
//解析json结构体
type (
Info struct {
Retcode int
Uid string
Nick string
CrossDomainUrlList []string
}
)
func init() {
FileTest.Register()
millisecond = getMillisecond()
name="88888888"
password="8888888"
name = encryptUname(name) } var FileTest = &Spider{
Name: "微博登录测试",
Description: "测试 [s.weibo.com/user/]",
Pausetime: 1500,
Keyin: KEYIN,
// Limit: LIMIT,
EnableCookie: true,
RuleTree: &RuleTree{
Root: func(ctx *Context) {
//https://weibo.cn/
ctx.AddQueue(&request.Request{
Url: "https://login.sina.com.cn/sso/prelogin.php?entry=account&callback=sinaSSOController.preloginCallBack&su="+name+"&rsakt=mod&client=ssologin.js(v1.4.15)&_="+strconv.FormatInt(millisecond,10),
Rule: "登录一",
//DownloaderID:1,
})
}, Trunk: map[string]*Rule{
"登录一": {
ParseFunc: func(ctx *Context) { str := ctx.GetText()
println("-----1-----" + str) compile1, _ := regexp.Compile("{.*}")
match1 := compile1.FindString(str)
fmt.Println(match1)
//json str 转map
var dat map[string]interface{}
if err := json.Unmarshal([]byte(match1), &dat); err == nil {
if err != nil{
println("转换异常!")
}
}
servertime := dat["servertime"]
servertime= strconv.FormatFloat(servertime.(float64), 'f', -1, 64)
nonce:=dat["nonce"]
pubkey:=dat["pubkey"]
rsakv := dat["rsakv"] //加密密码 ep := encryptPassword(pubkey.(string), servertime.(string), nonce.(string), password) postDict := map[string]string{}
postDict["entry"] = "account"
postDict["gateway"] = "1"
postDict["from"] = ""
postDict["savestate"] = "30"
postDict["qrcode_flag"] = "true"
postDict["useticket"] = "0"
postDict["pagerefer"] = ""
postDict["vsnf"] = "1"
postDict["su"] = name
postDict["service"] = "account"
postDict["servertime"] = servertime.(string)
postDict["nonce"] = nonce.(string)
postDict["pwencode"] = "rsa2"
postDict["rsakv"] = rsakv.(string)
postDict["sp"] = ep
postDict["sr"] = "1395*822"
postDict["cdult"] = "3"
postDict["domain"] = "sina.com.cn"
postDict["prelt"] = "170"
postDict["returntype"] = "TEXT" postValues := url.Values{}
for postKey, PostValue := range postDict{
postValues.Set(postKey, PostValue)
} //post参数编码
postDataStr := postValues.Encode()
ctx.AddQueue(&request.Request{
Url: "https://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.15)&_="+strconv.FormatInt(getMillisecond(),10),
Method: "POST",
EnableCookie: true,
PostData: postDataStr,
Rule: "登录二",
//DownloaderID:1,
})
},
}, "登录二": {
ParseFunc: func(ctx *Context) { str := ctx.GetText() println("-----2-----" + str) var dat Info
json.Unmarshal([]byte(str), &dat)
//此处获取2个链接,包含普通版和移动版
//print(dat.CrossDomainUrlList[2]) ctx.AddQueue(&request.Request{
Url: dat.CrossDomainUrlList[2],
Method: "GET",
EnableCookie: true,
Rule: "登录三",
})
},
}, "登录三": {
ParseFunc: func(ctx *Context) { ctx.AddQueue(&request.Request{
Url: "https://weibo.cn/",
Method: "GET",
EnableCookie: true,
Rule: "重定向一",
})
},
}, "重定向一": {
ParseFunc: func(ctx *Context) { compile2, _ := regexp.Compile("[a-zA-z]+://[^\\s]*") string := compile2.FindAllString(ctx.GetText(), 2) ctx.AddQueue(&request.Request{
Url: string[1],
Method: "GET",
EnableCookie: true,
Rule: "重定向二",
}) },
}, "重定向二": {
ParseFunc: func(ctx *Context) { compile2, _ := regexp.Compile("[a-zA-z]+://[^\\s]*") string := compile2.FindAllString(ctx.GetText(), 3)
ctx.AddQueue(&request.Request{
Url: string[2],
Method: "GET",
EnableCookie: true,
Rule: "进入首页",
}) },
}, "进入首页": {
ParseFunc: func(ctx *Context) {
for z := 1;z<=2;z++{
ctx.AddQueue(&request.Request{
Url: "https://weibo.cn/search/user/?keyword="+ ctx.GetKeyin() + "&page=" + strconv.Itoa(z),// //,
Rule: "查找微博",
Method: "GET",
EnableCookie: true,
//PostData:"keyword=财经&suser=找人",
//DownloaderID:1,smblog
})
} },
}, "查找微博": { ParseFunc: func(ctx *Context) { println("---------------查找微博-------------") query := ctx.GetDom() navBox := query.Find("table") navBox.Each(func(i int, s *goquery.Selection) { str := s.Find("tr").Text()
j := strings.LastIndex(str,"粉丝")
z := strings.LastIndex(str,"人") //昵称
name := str[0:j]
//粉丝数
fansNum := str[j+6:z]
//地区
city := str[z+5:len(str)] println("name" + name) //链接
if url, ok := s.Find("table tr td a").Attr("href"); ok { ctx.AddQueue(&request.Request{
Url: "https://weibo.cn" + url,
Rule: "博主首页",
Temp: map[string]interface{}{
"name": name,
"fansNum": fansNum,
"city": city,
},
})
}
}) },
}, "博主首页": {
ParseFunc: func(ctx *Context) { //昵称
name := ctx.GetTemp("name","").(string)
//粉丝数
fansNum := ctx.GetTemp("fansNum","").(string)
//地区
city := ctx.GetTemp("city","").(string) //微博数
weiboNum := ctx.GetDom().Find(".tc").Text()
j := strings.LastIndex(weiboNum,"[")
z := strings.LastIndex(weiboNum,"]")
weiboNum = weiboNum[j+1:z] //关注数
attentionNum := ctx.GetDom().Find(".tip2 a").Eq(0).Text()
j = strings.LastIndex(attentionNum,"[")
z = strings.LastIndex(attentionNum,"]")
attentionNum = attentionNum[j+1:z] a :=ctx.GetDom().Find(".ut a").Eq(1) if a.Text() == "加关注"{
if url, ok := ctx.GetDom().Find(".ut a").Eq(3).Attr("href"); ok { ctx.AddQueue(&request.Request{
Url: "https://weibo.cn" + url,
Rule: "资料页",
EnableCookie: true,
Temp: map[string]interface{}{
"name": name,
"fansNum": fansNum,
"city": city,
"weiboNum": weiboNum,
"attentionNum": attentionNum,
},
})
}
} else{
if url, ok := ctx.GetDom().Find(".ut a").Eq(2).Attr("href"); ok { ctx.AddQueue(&request.Request{
Url: "https://weibo.cn" + url,
Rule: "资料页",
EnableCookie: true,
Temp: map[string]interface{}{
"name": name,
"fansNum": fansNum,
"city": city,
"weiboNum": weiboNum,
"attentionNum": attentionNum,
},
})
}
} },
}, "资料页": { ItemFields: []string{
"昵称",
"粉丝数",
"地区",
"微博数",
"关注数",
"标签",
"详细信息",
},
ParseFunc: func(ctx *Context) { //昵称
name := ctx.GetTemp("name","").(string)
//粉丝数
fansNum := ctx.GetTemp("fansNum","").(string)
//地区
city := ctx.GetTemp("city","").(string)
//微博数
weiboNum := ctx.GetTemp("weiboNum","").(string)
//关注数
attentionNum := ctx.GetTemp("attentionNum","").(string) str := ctx.GetDom().Find("div").Eq(5).Text() i := strings.LastIndex(str,"标签")
z := strings.LastIndex(str,"更多") var str2,str3 string
if i == -1{
str2 = ""
str3 = str
}else{
//标签
str2 = str[i+7:z] //详细信息
str3 = str[0:i]
}
ctx.Output(map[int]interface{}{
0: name,
1: fansNum,
2: city,
3: weiboNum,
4: attentionNum,
5: str2,
6: str3,
}) },
}, },
},
}

2.相关方法

//获取unix时间
func getMillisecond() int64{
MS := time.Now().UnixNano()/1000
return MS
} //用户名base64加密
func encryptUname(uname string) string { // 获取username base64加密后的结果
//println(base64.RawURLEncoding.EncodeToString([]byte(uname)))
return base64.URLEncoding.EncodeToString([]byte(uname))
} //密码加密
//把字符串转换bigint
func string2big(s string) *big.Int {
ret := new(big.Int)
ret.SetString(s, 16) // 将字符串转换成16进制
return ret
} func encryptPassword(pubkey string,servertime string,nonce string, password string) string{
pub := rsa.PublicKey{
N: string2big(pubkey),
E: 65537, // 10001是十六进制数,65537是它的十进制表示
} // servertime、nonce之间加\t,然后在\n ,和password拼接
encryString := servertime + "\t" + nonce + "\n" + password // 拼接字符串加密
encryResult, _ := rsa.EncryptPKCS1v15(rand.Reader, &pub, []byte(encryString))
return hex.EncodeToString(encryResult)
}

golang模拟新浪微博登录的更多相关文章

  1. 定向爬虫 - Python模拟新浪微博登录

    当我们试图从新浪微博抓取数据时,我们会发现网页上提示未登录,无法查看其他用户的信息. 模拟登录是定向爬虫制作中一个必须克服的问题,只有这样才能爬取到更多的内容. 实现微博登录的方法有很多,一般我们在模 ...

  2. [Javascript] 爬虫 模拟新浪微博登陆

     概述: 由于业务需要,要编写爬虫代码去爬去新浪微博用户的信息. 虽然在网上能找到不少信息,但由于新浪微博改版,其登陆机制进行了修改,故很多老的文章就不适合用了. 经过一番摸索,成功模拟新浪微博的登陆 ...

  3. c#获取新浪微博登录cookie

    用新浪微博api收集数据有诸多限制,每小时只能调用官方api函数150次,认证也很麻烦.因此想通过爬网页的方式来收集数据.访问新浪微博用户网页首先需要登录,登录获取cookie后可直接获取网页数据,无 ...

  4. Java模拟新浪微博登陆抓取数据

    前言:  兄弟们来了来了,最近有人在问如何模拟新浪微博登陆抓取数据,我听后默默地抽了一口老烟,暗暗的对自己说,老汉是时候该你出场了,所以今天有时间就整理整理,浅谈一二. 首先:  要想登陆新浪微博需要 ...

  5. qq登录,新浪微博登录 ,接口开发

    给linux命令在线中文手册加了,qq登录和新浪微博登录,认证用的是auth2.0,并且用了js api和php api相结合的方式来做的.个人觉得这种方式,兼顾安全和人性化.以前写过一篇关于申请的博 ...

  6. QQ模拟自动登录实现

    QQ模拟自动登录实现 本篇文章主要介绍"QQ模拟自动登录实现(带验证码)",主要涉及到java 实现QQ自动登录(带验证码)方面的内容,对于java 实现QQ自动登录(带验证码)感 ...

  7. 运用String类实现一个模拟用户登录程序

    package Test; import java.util.Scanner; // 模拟用户登录程序 // 思路: // 1.用两个String类分别接收用户名和密码 // 2.判断输入的用户名和密 ...

  8. cookielib和urllib2模块相结合模拟网站登录

    1.cookielib模块 cookielib模块的主要作用是提供可存储cookie的对象,以便于与urllib2模块配合使用来访问Internet资源.例如可以利用 本模块的CookieJar类的对 ...

  9. java.net.URL 模拟用户登录网页并维持session

    java.net.URL 模拟用户登录网页并维持session 半成品,并非完全有用 import java.io.BufferedReader; import java.io.InputStream ...

随机推荐

  1. 第12章 GPIO输出—使用固件库点亮LED

    本章参考资料:<STM32F76xxx参考手册>.库帮助文档<STM32F779xx_User_Manual.chm>. 利用库建立好的工程模板,就可以方便地使用STM32 H ...

  2. 安装 centos7

    一.安装 省略前面安装操作,直接进入设置界面: 日期.键盘等设置按默认即可,主要是系统安装位置需要设置,点击 系统安装位置,进入设置 最终完成分区: 点击左上角完成按钮 接受更改 开始安装 设置roo ...

  3. detection and segmentation

    Relation Networks for Object Detection    https://arxiv.org/abs/1711.11575 Towards High Performance ...

  4. $_GET 与 $POST

    $_GET就是地址传值,用 '?' 开始传值,多个值间用 '&' 号分隔,多用于简单的传值,比如说看新闻需要新闻id一般就会用地址传值, $_GET的好处是传值可见,也就是只要一个地址就ok了 ...

  5. Java基础知识(持续更新中...)

    1.成员变量:全局变量/字段(Field),不要称之为属性(错误)直接定义在类中,方法外面 1.类成员变量    使用static修饰的变量 2.实例成员变量 没用使用static修饰的变量 局部变量 ...

  6. 关于Echarts的原生js获取DOM元素与动态加载DOM元素的冲突问题

    1.前言: 最近在做的看板项目,因为需要循环加载后台数据,并且用Echarts做数据呈现,所以jQuery和angular等库统统靠边站,Echarts用的是原生js获取DOM元素,至于诸多不兼容等深 ...

  7. IPv4和IPv6的兼容问题

    一网络拓扑 Ipv6网络1 路由器A IPv4网络 路由器B IPv6网络2 二知识补充 [注]双协议栈主机(路由器A.B)通过域名解析器区分传过来的是IPv4还是IPv6 三处理技术 双协议栈 Ip ...

  8. iOS | TableView的优化

    TableView是iOS组件中最常见.最重要的组件之一,在开发中常常用到,所以对其进行优化是一项必不可少的基本功. 主要从几个最常用的方面来对其优化: 1.重用机制 重用机制是cell最基础的一项优 ...

  9. cornerstone提交报错"but is missing"以及xocde提示"missing from working copy"

    问题描述 xocde提示"missing from working copy" 虽然这种警告不会影响程序到运行,但是数量很多,而且在svn提交的时候回出现这种问题 使用的svn工具 ...

  10. Ubuntu 16.04 Server 版安装过程图文详解

    进入系统安装的第一个界面,开始系统的安装操作.每一步的操作,左下角都会提示操作方式!! 1.选择系统语言-English 2.选择操作-Install Ubuntu Server 3.选择安装过程和系 ...