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 intUid stringNick stringCrossDomainUrlList []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 转mapvar 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"] = namepostDict["service"] = "account"postDict["servertime"] = servertime.(string)postDict["nonce"] = nonce.(string)postDict["pwencode"] = "rsa2"postDict["rsakv"] = rsakv.(string)postDict["sp"] = eppostDict["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 Infojson.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 stringif 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()/1000return 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) }
转载于:https://www.cnblogs.com/gaoyawei/p/7463766.html
golang模拟新浪微博登录相关推荐
- php模拟关注微博,PHP基于laravel框架获取微博数据之一 模拟新浪微博登录
参考资料: http://www.csuldw.com/2016/11/10/2016-11-10-simulate-sina-login/ http://blog.csdn.net/fly_leop ...
- php rsa2 微博,PHP 基于laravel框架获取微博数据之一 模拟新浪微博登录
模拟新浪微博登录是抓取新浪数据的基础,网上的参考资料大多介绍的是用Python开发,有一篇使用php模拟登录的资料还是在phpcms中实现的,也没有太深入分析. PS:网上资料来源比较乱,不知道php ...
- rsa加密算法python_模拟新浪微博登录(Python+RSA加密算法)
声明: 由于本人使用用的是Python语言,以下内容就在该语言下进行解释说明.有使用Java语言的可以参考IT男杂记(http://marspring.mobi/http-client-weibo/) ...
- 全程模拟新浪微博登录(2015)
很久之前就了解过模拟登录的过程,最近对python用的比较多,想来练练手,就想实现一下新浪微博登录,首先随便一搜,网上有大量的前辈们都做过了,我也仔细看了一下,并且参考之后发现无法登录,而且还有很多细 ...
- spirngmvc如何实现直接输入网页重定向到登录_Python 模拟新浪微博登录
点击上方"小猿学 Python",选择"置顶公众号" 作者:北岛知寒 链接:https://www.cnblogs.com/crazyacking/p/5232 ...
- [置顶]定向爬虫 - Python模拟新浪微博登录
当我们试图从新浪微博抓取数据时,我们会发现网页上提示未登录,无法查看其他用户的信息. 模拟登录是定向爬虫制作中一个必须克服的问题,只有这样才能爬取到更多的内容. 实现微博登录的方法有很多,一般我们在模 ...
- 微博登录记录pythonurllib_定向爬虫 - Python模拟新浪微博登录
当我们试图从新浪微博抓取数据时,我们会发现网页上提示未登录,无法查看其他用户的信息. 模拟登录是定向爬虫制作中一个必须克服的问题,只有这样才能爬取到更多的内容. 实现微博登录的方法有很多,一般我们在模 ...
- 模拟新浪微博登录:从原理分析到实现
上一篇文章小试牛刀:使用Python模拟登录知乎介绍了如何模拟知乎登录,虽然用到了验证码信息,但请求的参数都是原封不动的传递,刚开始接触的时候,觉得难度适中,回头再看的时候,反而感觉挺容易的.在这篇文 ...
- 模拟新浪微博登录-原理分析到实现
原文地址:http://www.csuldw.com/2016/11/10/2016-11-10-simulate-sina-login/ 上一篇文章 小试牛刀:使用Python模拟登录知乎 介绍了如 ...
- 模拟新浪微博登录发送微博
2015-10-14 应该是新浪微博更新,导致项目中之前开发的发送未必代码不能用了,结合网上的案例,修改了点代码重新整合下.代码看附件. 注意点: 模拟登陆时 postMethod2.addParam ...
最新文章
- python32位安装
- Name与x:Name的关系--转载
- centos 安装mysql5.7_Zabbix 4.2.5 安装部署实践详解
- elasticsearch 第二篇(配置篇)
- 【网址收藏】PowerShell因为在此系统中禁止执行脚本的解决方法
- 北京招聘 | 新浪微博机器学习部门招聘推荐算法开发实习生
- element vue 获取select 的label_Vue动态组件component的深度使用
- 【JAVA 第三章 流程控制语句】课后习题 键入日期输入星期几
- 创建初始脚本工具mkscript.sh
- Centos7安装Redis4.0.8
- python破解excel进入密码(密码字典)
- 山东工商学院 计算机科学与技术,实验中心-山东工商学院计算机科学与技术学院...
- 三步教会你在solidworks中画铝型材
- 2023中兴笔试复盘
- Data URL和图片
- Hibernate VS iBATIS (转自ITEYE davy)
- 如何删除双系统中的ubuntu
- 北京卓镭激光完成近亿元B轮融资,君联资本领投
- RJ TextEd 中文绿色版下载
- 手把手教你建立微信公众平台