golang模拟新浪微博登录
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模拟新浪微博登录的更多相关文章
- 定向爬虫 - Python模拟新浪微博登录
当我们试图从新浪微博抓取数据时,我们会发现网页上提示未登录,无法查看其他用户的信息. 模拟登录是定向爬虫制作中一个必须克服的问题,只有这样才能爬取到更多的内容. 实现微博登录的方法有很多,一般我们在模 ...
- [Javascript] 爬虫 模拟新浪微博登陆
概述: 由于业务需要,要编写爬虫代码去爬去新浪微博用户的信息. 虽然在网上能找到不少信息,但由于新浪微博改版,其登陆机制进行了修改,故很多老的文章就不适合用了. 经过一番摸索,成功模拟新浪微博的登陆 ...
- c#获取新浪微博登录cookie
用新浪微博api收集数据有诸多限制,每小时只能调用官方api函数150次,认证也很麻烦.因此想通过爬网页的方式来收集数据.访问新浪微博用户网页首先需要登录,登录获取cookie后可直接获取网页数据,无 ...
- Java模拟新浪微博登陆抓取数据
前言: 兄弟们来了来了,最近有人在问如何模拟新浪微博登陆抓取数据,我听后默默地抽了一口老烟,暗暗的对自己说,老汉是时候该你出场了,所以今天有时间就整理整理,浅谈一二. 首先: 要想登陆新浪微博需要 ...
- qq登录,新浪微博登录 ,接口开发
给linux命令在线中文手册加了,qq登录和新浪微博登录,认证用的是auth2.0,并且用了js api和php api相结合的方式来做的.个人觉得这种方式,兼顾安全和人性化.以前写过一篇关于申请的博 ...
- QQ模拟自动登录实现
QQ模拟自动登录实现 本篇文章主要介绍"QQ模拟自动登录实现(带验证码)",主要涉及到java 实现QQ自动登录(带验证码)方面的内容,对于java 实现QQ自动登录(带验证码)感 ...
- 运用String类实现一个模拟用户登录程序
package Test; import java.util.Scanner; // 模拟用户登录程序 // 思路: // 1.用两个String类分别接收用户名和密码 // 2.判断输入的用户名和密 ...
- cookielib和urllib2模块相结合模拟网站登录
1.cookielib模块 cookielib模块的主要作用是提供可存储cookie的对象,以便于与urllib2模块配合使用来访问Internet资源.例如可以利用 本模块的CookieJar类的对 ...
- java.net.URL 模拟用户登录网页并维持session
java.net.URL 模拟用户登录网页并维持session 半成品,并非完全有用 import java.io.BufferedReader; import java.io.InputStream ...
随机推荐
- 复合词(Compound Words, UVa 10391)(stl set)
You are to find all the two-word compound words in a dictionary. A two-word compound word is a word i ...
- 开始重学java【门头沟2017年12月6日】
现在从struts2开始重新学习java, 想找个人一起学习java/php都可以. 学习时间大概是两个月,就是年前这段时间. 下一个阶段就是做项目进行练习.(时间为:一个月时间) 不管是学习java ...
- 火狐 SSL 收到了一个弱临时 Diffie-Hellman 密钥
火狐 SSL 收到了一个弱临时 Diffie-Hellman 密钥 最近在用FireFox 调试时使用Https,连接 https网址 时发生错误. 在服务器密钥交换握手信息中 SSL 收到了一个 ...
- PL/SQL 条件控制语句
1.if-then 语句 语法: IF 条件 THEN 语句序列; END IF; 实例: DECLARE i ) :; BEGIN THEN dbms_output.put_line('True') ...
- 通过匿名管道获取CMD运行结果
#include <iostream> #include <string> #include <Windows.h> using namespace std; /* ...
- 【原创】os.chdir设置的工作路径和sys.path之间到底是个啥关系?
转载请注明出处:https://www.cnblogs.com/oceanicstar/p/9390455.html 直接放上测试后的结论(测试代码和截图过多,有兴趣的小伙伴可自己测试,未来看情况 ...
- 关于ECharts内存泄漏问题
最近使用websocket加ECharts做了一个实时监控的功能,发现了一个比较严重的问题,就是浏览器运行一段时间就会非常卡,之前在ECharts官网运行官方实例“动态数据 + 时间坐标轴”时,也遇到 ...
- 在ReactNative中使用Typescript
在ReactNative中使用Typescript 少侠放心,跟着我的这个步骤走,保你完美在RN项目中使用Typescript,废话不多说,走你 1.全局安装create-react-native-a ...
- HBase学习(三):数据模型
和传统的关系型数据库类似,HBase以表(Table)的方式组织数据.HBase的表由行(Row)和列(Column)共同构成,与关系型数据库不同的是HBase有一个列族(ColumnFamily)的 ...
- keil5 mdk调用外部编辑器notepad++、sublime3、VSCode总结
1.打开keil主界面,点击菜单栏Tools菜单,选择如下图所示的选项. 2.点击如下图所示的菜单上红笔标注的地方,给这个工具命名,如notepad++.sublime3.vscode等,如下图, 并 ...