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. 【luogu P1774 最接近神的人_NOI导刊2010提高(02)】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1774 归并排序求逆序对. #include <cstdio> #define livelove ...

  2. mysql自连接

    自连接: -- 求7369员工编号,姓名,经理编号和经理姓名 select e1.empno,e1.ename,e2.empno,e2.ename from emp e1,emp e2 where e ...

  3. DataTables的相关问题集锦

    1.修改头部问题,列表加载完重新修改表头内容 使用回调函数: headerCallback: function( thead, data, start, end, display ) {        ...

  4. SpringBoot学习16:springboot整合junit单元测试

    1.创建maven项目,修改pom.xml文件 <!--springboot项目依赖的父项目--> <parent> <groupId>org.springfram ...

  5. iOS开发 | 自定义不规则label

    其中有一个不太规则的label:   image.png 这个label顶部的两个角是圆角,底部的两个角是直角,底部还有一个小三角. 思路 CAShapeLayer联合UIBezierPath画一个不 ...

  6. 『ACM C++』 PTA 天梯赛练习集L1 | 012-015

    女神节快乐鸭,大学的女生节真的是忙碌呢,到处送礼物,真的是当时男生节的出来混的,总该是要还的hhhhh ------------------------------------------------ ...

  7. 内存分配---FF、BF、WF三种算法

    动态分区分配是根据进程的实际需要,动态的为之分配内存空间.而在实现可变分区分配时,将涉及到分区分配中 所用的数据结构.分区分配算法和分区的分配与内存回收的过程. 分区分配中的数据结构:(1)描述空闲块 ...

  8. iptables应用

    192.168.4.119 为本机的ip地址:每条链的规则是由上至下进行匹配,因此我们需要把范围小的规则放在上面以防被覆盖. 1)清空iptables默认规则,并自定义规则 [root@iptable ...

  9. ubuntu以root进入图形化界面

    sudo nautilus 可以进行一些文件夹移动操作,不会出现权限的问题

  10. ARM串口控制终端命令

    配置开发板eth0网络: # ifconfig eth0 10.70.12.168