python模拟登录百度贴吧(百度贴吧登录)实例

yipeiwu_com5年前Python基础

 

复制代码 代码如下:

# -*- coding:utf-8 -*-
# python3.3.3

import sys,time,re,urllib.parse,urllib.request,http.cookiejar,random,math,os.path,hashlib,json,binascii,threading

"""cookie"""
cookie=http.cookiejar.LWPCookieJar()
#cookie.load('f:/cookie.txt',True,True)
chandle=urllib.request.HTTPCookieProcessor(cookie)
"""获取数据"""
def getData(url):
    r=urllib.request.Request(url)
    opener=urllib.request.build_opener(chandle)
    u=opener.open(r)
    #chandle.cookiejar.save('f:/cookie.txt',True,True)
    data=u.read()
    try:
        data=data.decode('utf-8')
    except:
        data=data.decode('gbk','ignore')
    return data
def postData(url,data):
    data=urllib.parse.urlencode(data);data=bytes(data,'utf-8')
    r=urllib.request.Request(url,data)
    opener=urllib.request.build_opener(chandle)
    u=opener.open(r)
    #chandle.cookiejar.save('f:/cookie.txt',True,True)
    data=u.read()
    try:
        data=data.decode('utf-8')
    except:
        data=data.decode('gbk','ignore')
    return data
def login(name,pwd):
    url='http://www.baidu.com'
    getData(url)
    par={
        "apiver":'v3',
        "callback":'bd__cbs__oug2fy',
        "class":'login',
        "logintype":'dialogLogin',
        "tpl":'tb',
        "tt":'1385013373144'
    }
    url='https://passport.baidu.com/v2/api/?getapi&%s' % urllib.parse.urlencode(par)
    token=re.findall('"token" : "(.*?)"',getData(url))[0]
    par.update({"isphone":'false',"username":name,"token":token})
    url='https://passport.baidu.com/v2/api/?logincheck&?%s' % urllib.parse.urlencode(par)
    data={
        "charset":'GBK',
        "mem_pass":'on',
        "password":pwd,
        "ppui_logintime":'1612376',
        "quick_user":'0',
        "safeflg":'0',
        "splogin":'rate',
        "u":'http://tieba.baidu.com/'
    }
    url='https://passport.baidu.com/v2/api/?login'
    par.update(data)
    bdu=re.findall('hao123Param=(.*?)&',postData(url,par))[0]
    par={
        "bdu":bdu,
        "t":'1385013373144'
    }
    url='http://user.hao123.com/static/crossdomain.php?%s' % urllib.parse.urlencode(par)
    getData(url)
    print(json.loads(getData('http://tieba.baidu.com/f/user/json_userinfo')))
"""------输入帐号密码------"""
login('帐号','密码')

复制代码 代码如下:

# -*- coding:utf-8 -*-
# python3.3.3

import sys,time,re,urllib.parse,urllib.request,http.cookiejar,random,math,os.path,hashlib,json,binascii,threading

"""cookie"""
cookie=http.cookiejar.LWPCookieJar()
#cookie.load('f:/cookie.txt',True,True)
chandle=urllib.request.HTTPCookieProcessor(cookie)
"""获取数据"""
def getData(url):
    r=urllib.request.Request(url)
    opener=urllib.request.build_opener(chandle)
    u=opener.open(r)
    #chandle.cookiejar.save('f:/cookie.txt',True,True)
    data=u.read()
    try:
        data=data.decode('utf-8')
    except:
        data=data.decode('gbk','ignore')
    return data
def postData(url,data):
    data=urllib.parse.urlencode(data);data=bytes(data,'utf-8')
    r=urllib.request.Request(url,data)
    opener=urllib.request.build_opener(chandle)
    u=opener.open(r)
    #chandle.cookiejar.save('f:/cookie.txt',True,True)
    data=u.read()
    try:
        data=data.decode('utf-8')
    except:
        data=data.decode('gbk','ignore')
    return data
def login(name,pwd):
    url='http://www.baidu.com'
    getData(url)
    par={
        "apiver":'v3',
        "callback":'bd__cbs__oug2fy',
        "class":'login',
        "logintype":'dialogLogin',
        "tpl":'tb',
        "tt":'1385013373144'
    }
    url='https://passport.baidu.com/v2/api/?getapi&%s' % urllib.parse.urlencode(par)
    token=re.findall('"token" : "(.*?)"',getData(url))[0]
    par.update({"isphone":'false',"username":name,"token":token})
    url='https://passport.baidu.com/v2/api/?logincheck&?%s' % urllib.parse.urlencode(par)
    data={
        "charset":'GBK',
        "mem_pass":'on',
        "password":pwd,
        "ppui_logintime":'1612376',
        "quick_user":'0',
        "safeflg":'0',
        "splogin":'rate',
        "u":'http://tieba.baidu.com/'
    }
    url='https://passport.baidu.com/v2/api/?login'
    par.update(data)
    bdu=re.findall('hao123Param=(.*?)&',postData(url,par))[0]
    par={
        "bdu":bdu,
        "t":'1385013373144'
    }
    url='http://user.hao123.com/static/crossdomain.php?%s' % urllib.parse.urlencode(par)
    getData(url)
    print(json.loads(getData('http://tieba.baidu.com/f/user/json_userinfo')))
"""------输入帐号密码------"""
login('帐号','密码')

相关文章

Python实现二分法算法实例

1.算法:(设查找的数组期间为array[low, high]) (1)确定该期间的中间位置K (2)将查找的值T与array[k]比较。若相等,查找成功返回此位置;否则确定新的查找区域...

Python导入txt数据到mysql的方法

本文实例讲述了Python导入txt数据到mysql的方法。分享给大家供大家参考。具体分析如下: 从TXT文本转换数据到MYSQL数据库,接触一段时间python了 第一次写东西 用的是...

浅谈Python使用Bottle来提供一个简单的web服务

介绍 今天有个不正经的需求,就是要快速做一个restful api的性能测试,要求测试在海量作业数据的情况下客户端分页获取所有作业的性能。因为只是一个小的的测试工作,所以就想到了Bott...

centos6.4下python3.6.1安装教程

centos6.4下python3.6.1安装教程

本文实例为大家分享了python3.6.1安装教程,供大家参考,具体内容如下 1、安装编译环境所需包 #yum install zlib-devel bzip2-devel open...

python使用正则表达式匹配字符串开头并打印示例

本文实例讲述了python使用正则表达式匹配字符串开头并打印的方法。分享给大家供大家参考,具体如下: import re s="name=z1hangshan username=ff...