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爬虫框架Scrapy实例代码
Mar 04 Python
python日期时间转为字符串或者格式化输出的实例
May 29 Python
django利用request id便于定位及给日志加上request_id
Aug 26 Python
在Python中通过getattr获取对象引用的方法
Jan 21 Python
Python socket模块实现的udp通信功能示例
Apr 10 Python
Python实现获取系统临时目录及临时文件的方法示例
Jun 26 Python
python 实现提取log文件中的关键句子,并进行统计分析
Dec 24 Python
pytorch使用 to 进行类型转换方式
Jan 08 Python
Python尾递归优化实现代码及原理详解
Oct 09 Python
关于Python 解决Python3.9 pandas.read_excel(‘xxx.xlsx‘)报错的问题
Nov 28 Python
用python制作个视频下载器
Feb 01 Python
一劳永逸彻底解决pip install慢的办法
May 24 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
Netflix将与CLAMP、乙一以及冲方丁等6名知名制作人合伙展开原创动画计划!
2020/03/06 日漫
PHP 数组教程 定义数组
2009/10/23 PHP
php操作MongoDB基础教程(连接、新增、修改、删除、查询)
2014/03/25 PHP
php对微信支付回调处理的方法
2018/08/23 PHP
Laravel自动生成UUID,从建表到使用详解
2019/10/24 PHP
多个iframe自动调整大小的问题
2006/09/18 Javascript
WordPress JQuery处理沙发头像
2009/06/22 Javascript
js下获取div中的数据的原理分析
2010/04/07 Javascript
JavaScript判断窗口是否最小化的代码(跨浏览器)
2010/08/01 Javascript
javascript cookie基础应用之记录用户名的方法
2016/09/20 Javascript
JS动态给对象添加属性和值的实现方法
2016/10/21 Javascript
Ajax跨域实现代码(后台jsp)
2017/01/21 Javascript
JavaScript验证知识整理
2017/03/24 Javascript
vue2实现移动端上传、预览、压缩图片解决拍照旋转问题
2017/04/13 Javascript
angular使用post、get向后台传参的问题实例
2017/05/27 Javascript
Bootstrap 模态框多次显示后台提交多次BUG的解决方法
2017/12/26 Javascript
layui点击导航栏刷新tab页的示例代码
2018/08/14 Javascript
node.js环境搭建图文详解
2018/09/19 Javascript
vue+VeeValidate 校验范围实例详解(部分校验,全部校验)
2018/10/19 Javascript
python将多个文本文件合并为一个文本的代码(便于搜索)
2011/03/13 Python
用Python实现一个简单的能够发送带附件的邮件程序的教程
2015/04/08 Python
Python实现的插入排序算法原理与用法实例分析
2017/11/22 Python
Python解决走迷宫问题算法示例
2018/07/27 Python
python中dir()与__dict__属性的区别浅析
2018/12/10 Python
使用Python实现企业微信的自动打卡功能
2019/04/30 Python
Python数据可视化实现正态分布(高斯分布)
2019/08/21 Python
python自动结束mysql慢查询会话的实例代码
2019/10/27 Python
简单了解python shutil模块原理及使用方法
2020/04/28 Python
翻转数列python实现,求前n项和,并能输出整个数列的案例
2020/05/03 Python
解决Ubuntu18中的pycharm不能调用tensorflow-gpu的问题
2020/09/17 Python
python利用 keyboard 库记录键盘事件
2020/10/16 Python
美国电子元器件分销商:Newark element14
2018/01/13 全球购物
英国在线电子和小工具商店:TecoBuy
2018/10/06 全球购物
爱护公共设施的标语
2014/06/24 职场文书
2014教师党员个人自我评议
2014/09/20 职场文书
服务员岗位职责范本
2015/04/09 职场文书