亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標題: python3模擬百度登錄并實現(xiàn)貼吧自動簽到 [打印本頁]

作者: mahaijiang    時間: 2015-07-08 14:16
標題: python3模擬百度登錄并實現(xiàn)貼吧自動簽到
baiduclient.py
  1. '''
  2. Created on 2014-2-20

  3. @author: Vincent
  4. '''
  5. import urllib.parse
  6. import gzip
  7. import json
  8. import re
  9. from http.client import HTTPConnection
  10. from htmlutils import TieBaParser
  11. import httputils as utils

  12. # 請求頭
  13. headers = dict()
  14. headers["Connection"] = "keep-alive"
  15. headers["Cache-Control"] = "max-age=0"
  16. headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
  17. 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"
  18. headers["Content-Type"] = "application/x-www-form-urlencoded"
  19. headers["Accept-Encoding"] = "gzip,deflate,sdch"
  20. headers["Accept-Language"] = "zh-CN,zh;q=0.8"
  21. headers["Cookie"] = ""

  22. # cookie
  23. cookies = list()

  24. # 個人信息
  25. userInfo = {}

  26. def login(account, password):
  27.     '''登錄'''
  28.     global cookies
  29.     headers["Host"] = "wappass.baidu.com"
  30.     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="
  31.     body = body.format(account, password)
  32.     conn = HTTPConnection("wappass.baidu.com", 80)
  33.     conn.request("POST", "/passport/login", body, headers)
  34.     resp = conn.getresponse()
  35.     cookies += utils.getCookiesFromHeaders(resp.getheaders())
  36.     utils.saveCookies(headers, cookies)
  37.     # 登錄成功會返回302
  38.     return True if resp.code == 302 else False
  39.      

  40. def getTieBaList():
  41.     '''獲取已關注的貼吧列表'''
  42.     conn = HTTPConnection("tieba.baidu.com", 80)
  43.     conn.request("GET", "/mo/m?tn=bdFBW&tab=favorite", "", headers)
  44.     resp = conn.getresponse()   
  45.     tieBaParser = TieBaParser()
  46.     tieBaParser.feed(resp.read().decode())
  47.     tbList = tieBaParser.getTieBaList()
  48.     return tbList
  49.      

  50. def getSignInfo(tieBaName):
  51.     '''獲取貼吧簽到信息'''
  52.     queryStr = urllib.parse.urlencode({"kw":tieBaName, "ie":"utf-8", "t":0.571444})
  53.     conn = HTTPConnection("tieba.baidu.com", 80)
  54.     conn.request("GET", "/sign/loadmonth?" + queryStr, "", headers)
  55.     data = gzip.decompress(conn.getresponse().read()).decode("GBK")
  56.     signInfo = json.loads(data)
  57.     return signInfo
  58.      
  59.       
  60. tbsPattern = re.compile('"tbs" value=".{20,35}"')

  61. def signIn(tieBaName):
  62.     '''簽到'''
  63.     # 獲取頁面中的參數(shù)tbs
  64.     conn1 = HTTPConnection("tieba.baidu.com", 80)
  65.     queryStr1 = urllib.parse.urlencode({"kw": tieBaName})
  66.     conn1.request("GET", "/mo/m?" + queryStr1, "", headers)
  67.     html = conn1.getresponse().read().decode()
  68.     tbs = tbsPattern.search(html).group(0)[13:-1]
  69.     # 簽到
  70.     conn2 = HTTPConnection("tieba.baidu.com", 80)
  71.     body = urllib.parse.urlencode({"kw":tieBaName, "tbs":tbs, "ie":"utf-8"})
  72.     conn2.request("POST", "/sign/add" , body , headers)
  73.     resp2 = conn2.getresponse()
  74.     data = json.loads((gzip.decompress(resp2.read())).decode())
  75.     return data
  76.      

  77. def getUserInfo():
  78.     '''獲取個人信息'''
  79.     headers.pop("Host")
  80.     conn = HTTPConnection("tieba.baidu.com", 80)
  81.     conn.request("GET", "/f/user/json_userinfo", "", headers)
  82.     resp = conn.getresponse()
  83.     data = gzip.decompress(resp.read()).decode("GBK")
  84.     global userInfo
  85.     userInfo = json.loads(data)


  86. if __name__ == "__main__":
  87.     account = input("請輸入帳號:")
  88.     password = input("請輸入密碼:")
  89.    
  90.     ok = login(account, password)
  91.     if ok:
  92.         getUserInfo()
  93.         print(userInfo["data"]["user_name_weak"] + "~~~登錄成功", end="\n------\n")
  94.         for tb in getTieBaList():
  95.             print(tb + "吧:")
  96.             signInfo = signIn(tb)
  97.             if signInfo["no"] != 0:
  98.                 print("簽到失敗!")
  99.                 print(signInfo["error"])
  100.             else:
  101.                 print("簽到成功!")
  102.                 print("簽到天數(shù):" + str(signInfo["data"]["uinfo"]["cout_total_sing_num"]))
  103.                 print("連續(xù)簽到天數(shù):" + str(signInfo["data"]["uinfo"]["cont_sign_num"]))
  104.             print("------")
  105.     else:
  106.         print("登錄失敗")
復制代碼
htmlutils.py
  1. '''
  2. Created on 2014-2-20

  3. @author: Vincent
  4. '''

  5. from html.parser import HTMLParser

  6. class TieBaParser(HTMLParser):
  7.     def __init__(self):
  8.         HTMLParser.__init__(self)
  9.         self.tieBaList = list()
  10.         self.flag = False
  11.          
  12.     def getTieBaList(self):
  13.         return self.tieBaList
  14.      
  15.     def handle_starttag(self, tag, attrs):
  16.         if tag == "a":
  17.             for name , value in attrs:
  18.                 if name == "href" and "m?kw=" in value:
  19.                     self.flag = True
  20.                         
  21.     def handle_data(self, data):
  22.         if self.flag:
  23.             self.tieBaList.append(data)
  24.             self.flag = False
復制代碼
httputils.py
  1. '''
  2. Created on 2014-2-20

  3. @author: Vincent
  4. '''
  5. def getCookiesFromHeaders(headers):
  6.     '''從http響應中獲取所有cookie'''
  7.     cookies = list()
  8.     for header in headers:
  9.         if "Set-Cookie" in header:
  10.             cookie = header[1].split(";")[0]
  11.             cookies.append(cookie)
  12.     return cookies
  13.          
  14. def saveCookies(headers, cookies):
  15.     '''保存cookies'''
  16.     for cookie in cookies:
  17.         headers["Cookie"] += cookie + ";"

  18. def getCookieValue(cookies, cookieName):
  19.     '''從cookies中獲取指定cookie的值'''
  20.     for cookie in cookies:
  21.         if cookieName in cookie:
  22.             index = cookie.index("=") + 1
  23.             value = cookie[index:]
  24.             return value
  25.          
  26. def parseQueryString(queryString):
  27.     '''解析查詢串'''
  28.     result = dict()
  29.     strs = queryString.split("&")
  30.     for s in strs:
  31.         name = s.split("=")[0]
  32.         value = s.split("=")[1]
  33.         result[name] = value
  34.     return result
復制代碼

作者: HH106    時間: 2015-07-08 16:20
學習了.......
作者: qinyiwang    時間: 2015-08-20 17:49
不錯,要mark一下~~~~~~~~
作者: yage9992003    時間: 2015-12-28 14:18
現(xiàn)在還好用么?




歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2