当前位置:  编程技术>python

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

    来源: 互联网  发布时间:2014-10-04

    本文导语:  baiduclient.py 代码如下:import urllib.parseimport gzipimport jsonimport refrom http.client import HTTPConnectionfrom htmlutils import TieBaParserimport httputils as utils # 请求头headers = dict()headers["Connection"] = "keep-alive"headers["Cache-Control"] = "max-age=0"headers["Accept"] = "...

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


    
 
 

您可能感兴趣的文章:

 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • Python3中内置类型bytes和str用法及byte和string之间各种编码转换
  • gvim7.3支持python3的问题
  • Python3通过request.urlopen实现Web网页图片下载
  • 新装python3在Ubuntu中左右键显示乱码
  • 在Python3中使用urllib实现http的get和post提交数据操作
  • Python3实现生成随机密码的方法
  • python3编写C/S网络程序实例教程
  • python3.3实现乘法表示例
  • python3.0 字典key排序
  • 一则python3的简单爬虫代码
  • Python3实现的腾讯微博自动发帖小工具
  • python3使用urllib示例取googletranslate(谷歌翻译)
  • Python3实现连接SQLite数据库的方法
  • 让python同时兼容python2和python3的8个技巧分享
  • python3图片转换二进制存入mysql
  • python3访问sina首页中文的处理方法
  • python3.3教程之模拟百度登陆代码分享
  • python3使用tkinter实现ui界面简单实例
  • Mac OS X10.9安装的Python2.7升级Python3.3步骤详解
  • Python3.0与2.X版本的区别实例分析
  • python3.3使用tkinter开发猜数字游戏示例


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3