python3模拟百度登录并实现百度贴吧签到示例分享(百度贴吧自动签到)


Posted in Python onFebruary 24, 2014

baiduclient.py

import urllib.parse
import gzip
import json
import re
from http.client import HTTPConnection
from htmlutils import TieBaParser
import httputils as utils
# 请求头
headers = dict()
headers["Connection"] = "keep-alive"
headers["Cache-Control"] = "max-age=0"
headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36"
headers["Content-Type"] = "application/x-www-form-urlencoded"
headers["Accept-Encoding"] = "gzip,deflate,sdch"
headers["Accept-Language"] = "zh-CN,zh;q=0.8"
headers["Cookie"] = ""
# cookie
cookies = list()
# 个人信息
userInfo = {}
def login(account, password):
    '''登录'''
    global cookies
    headers["Host"] = "wappass.baidu.com"
    body = "username={0}&password={1}&submit=%E7%99%BB%E5%BD%95&quick_user=0&isphone=0&sp_login=waprate&uname_login=&loginmerge=1&vcodestr=&u=http%253A%252F%252Fwap.baidu.com%253Fuid%253D1392873796936_247&skin=default_v2&tpl=&ssid=&from=&uid=1392873796936_247&pu=&tn=&bdcm=3f7d51b436d12f2e83389b504fc2d56285356820&type=&bd_page_type="
    body = body.format(account, password)
    conn = HTTPConnection("wappass.baidu.com", 80)
    conn.request("POST", "/passport/login", body, headers)
    resp = conn.getresponse()
    cookies += utils.getCookiesFromHeaders(resp.getheaders())
    utils.saveCookies(headers, cookies)
    # 登录成功会返回302
    return True if resp.code == 302 else False
    
def getTieBaList():
    '''获取已关注的贴吧列表'''
    conn = HTTPConnection("tieba.baidu.com", 80)
    conn.request("GET", "/mo/m?tn=bdFBW&tab=favorite", "", headers)
    resp = conn.getresponse()    
    tieBaParser = TieBaParser()
    tieBaParser.feed(resp.read().decode())
    tbList = tieBaParser.getTieBaList()
    return tbList
    
def getSignInfo(tieBaName):
    '''获取贴吧签到信息'''
    queryStr = urllib.parse.urlencode({"kw":tieBaName, "ie":"utf-8", "t":0.571444})
    conn = HTTPConnection("tieba.baidu.com", 80)
    conn.request("GET", "/sign/loadmonth?" + queryStr, "", headers)
    data = gzip.decompress(conn.getresponse().read()).decode("GBK")
    signInfo = json.loads(data)
    return signInfo
     
tbsPattern = re.compile('"tbs" value=".{20,35}"')
def signIn(tieBaName):
    '''签到'''
    # 获取页面中的参数tbs
    conn1 = HTTPConnection("tieba.baidu.com", 80)
    queryStr1 = urllib.parse.urlencode({"kw": tieBaName})
    conn1.request("GET", "/mo/m?" + queryStr1, "", headers)
    html = conn1.getresponse().read().decode()
    tbs = tbsPattern.search(html).group(0)[13:-1]
    # 签到
    conn2 = HTTPConnection("tieba.baidu.com", 80)
    body = urllib.parse.urlencode({"kw":tieBaName, "tbs":tbs, "ie":"utf-8"})
    conn2.request("POST", "/sign/add" , body , headers)
    resp2 = conn2.getresponse()
    data = json.loads((gzip.decompress(resp2.read())).decode())
    return data
    
def getUserInfo():
    '''获取个人信息'''
    headers.pop("Host")
    conn = HTTPConnection("tieba.baidu.com", 80)
    conn.request("GET", "/f/user/json_userinfo", "", headers)
    resp = conn.getresponse()
    data = gzip.decompress(resp.read()).decode("GBK")
    global userInfo
    userInfo = json.loads(data)

if __name__ == "__main__":
    account = input("请输入帐号:")
    password = input("请输入密码:") 
    ok = login(account, password)
    if ok:
        getUserInfo()
        print(userInfo["data"]["user_name_weak"] + "~~~登录成功", end="\n------\n")
        for tb in getTieBaList():
            print(tb + "吧:")
            signInfo = signIn(tb)
            if signInfo["no"] != 0:
                print("签到失败!")
                print(signInfo["error"])
            else:
                print("签到成功!")
                print("签到天数:" + str(signInfo["data"]["uinfo"]["cout_total_sing_num"]))
                print("连续签到天数:" + str(signInfo["data"]["uinfo"]["cont_sign_num"]))
            print("------") 
    else:
        print("登录失败")

htmlutils.py

'''
Created on 2014-2-20
@author: Vincent
'''
from html.parser import HTMLParser
class TieBaParser(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self.tieBaList = list()
        self.flag = False
    def getTieBaList(self):
        return self.tieBaList
    def handle_starttag(self, tag, attrs):
        if tag == "a":
            for name , value in attrs:
                if name == "href" and "m?kw=" in value:
                    self.flag = True
    def handle_data(self, data):
        if self.flag:
            self.tieBaList.append(data)
            self.flag = False

httputils.py

'''
Created on 2014-2-20
@author: Vincent
'''
def getCookiesFromHeaders(headers):
    '''从http响应中获取所有cookie'''
    cookies = list()
    for header in headers:
        if "Set-Cookie" in header:
            cookie = header[1].split(";")[0]
            cookies.append(cookie)
    return cookies
def saveCookies(headers, cookies):
    '''保存cookies'''
    for cookie in cookies:
        headers["Cookie"] += cookie + ";"
def getCookieValue(cookies, cookieName):
    '''从cookies中获取指定cookie的值'''
    for cookie in cookies:
        if cookieName in cookie:
            index = cookie.index("=") + 1
            value = cookie[index:]
            return value
def parseQueryString(queryString):
    '''解析查询串'''
    result = dict()
    strs = queryString.split("&")
    for s in strs:
        name = s.split("=")[0]
        value = s.split("=")[1]
        result[name] = value
    return result
Python 相关文章推荐
Python的print用法示例
Feb 11 Python
Python实现备份MySQL数据库的方法示例
Jan 11 Python
scrapy爬虫完整实例
Jan 25 Python
使用实现XlsxWriter创建Excel文件并编辑
May 04 Python
Python3爬虫学习之应对网站反爬虫机制的方法分析
Dec 12 Python
在python 不同时区之间的差值与转换方法
Jan 14 Python
pandas DataFrame 行列索引及值的获取的方法
Jul 02 Python
python 直接赋值和copy的区别详解
Aug 07 Python
Python csv模块使用方法代码实例
Aug 29 Python
pygame实现五子棋游戏
Oct 29 Python
Python Pandas 对列/行进行选择,增加,删除操作
May 17 Python
Python Flask异步发送邮件实现方法解析
Aug 01 Python
python实现socket客户端和服务端简单示例
Feb 24 #Python
python抓取网页内容示例分享
Feb 24 #Python
使用python装饰器验证配置文件示例
Feb 24 #Python
python通过urllib2爬网页上种子下载示例
Feb 24 #Python
python控制台显示时钟的示例
Feb 24 #Python
python3访问sina首页中文的处理方法
Feb 24 #Python
python原始套接字编程示例分享
Feb 21 #Python
You might like
如何正确理解PHP的错误信息
2006/10/09 PHP
PHP注释实例技巧
2008/10/03 PHP
PHP 错误之引号中使用变量
2009/05/04 PHP
使用PHP获取汉字的拼音(全部与首字母)
2013/06/27 PHP
一个简单且很好用的php分页类
2013/10/26 PHP
VPS中使用LNMP安装WordPress教程
2014/12/28 PHP
浅谈PHP中的
2016/04/23 PHP
PHP检测链接是否存在的代码实例分享
2016/05/06 PHP
Jquery getJSON方法详细分析
2013/12/26 Javascript
js数值和和字符串进行转换时可以对不同进制进行操作
2014/03/05 Javascript
Javascript基础教程之switch语句
2015/01/18 Javascript
JS组件Bootstrap实现弹出框和提示框效果代码
2015/12/08 Javascript
js和jQuery设置Opacity半透明 兼容IE6
2016/05/24 Javascript
清除js缓存的多种方法总结
2016/12/09 Javascript
基于jQuery实现手风琴菜单、层级菜单、置顶菜单、无缝滚动效果
2017/07/20 jQuery
BootStrap 标题设置跨行无效的解决方法
2017/10/25 Javascript
vue中父子组件注意事项,传值及slot应用技巧
2018/05/09 Javascript
微信小程序动态增加按钮组件
2018/09/14 Javascript
JS/HTML5游戏常用算法之路径搜索算法 A*寻路算法完整实例
2018/12/14 Javascript
简单实现节流函数和防抖函数过程解析
2019/10/08 Javascript
微信小程序实现电子签名并导出图片
2020/05/27 Javascript
JS如何判断对象是否包含某个属性
2020/08/29 Javascript
浅析vue中的nextTick
2020/12/28 Vue.js
详解JavaScript中的this指向问题
2021/02/05 Javascript
python 文件和路径操作函数小结
2009/11/23 Python
Python常用列表数据结构小结
2014/08/06 Python
Python语言的面相对象编程方式初步学习
2016/03/12 Python
解决pyshp UnicodeDecodeError的问题
2019/12/06 Python
python路径的写法及目录的获取方式
2019/12/26 Python
Python如何使用内置库matplotlib绘制折线图
2020/02/24 Python
使用python处理题库表格并转化为word形式的实现
2020/04/14 Python
电焊工工作岗位职责
2014/02/06 职场文书
和睦家庭事迹
2014/05/14 职场文书
2014年党的群众路线活动个人整改措施
2014/10/28 职场文书
ORACLE数据库应用开发的三十个注意事项
2021/06/07 Oracle
SpringBoot集成Redis的思路详解
2021/10/16 Redis