python3模拟百度登录并实现百度贴吧签到示例分享(百度贴吧自动签到)
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