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

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 13466 | 回復(fù): 3
打印 上一主題 下一主題

python3模擬百度登錄并實現(xiàn)貼吧自動簽到 [復(fù)制鏈接]

論壇徽章:
2
操作系統(tǒng)版塊每日發(fā)帖之星
日期:2015-06-26 22:20:00每日論壇發(fā)貼之星
日期:2015-06-26 22:20:00
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2015-07-08 14:16 |只看該作者 |倒序瀏覽
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.     '''獲取已關(guān)注的貼吧列表'''
  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("登錄失敗")
復(fù)制代碼
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
復(fù)制代碼
httputils.py
  1. '''
  2. Created on 2014-2-20

  3. @author: Vincent
  4. '''
  5. def getCookiesFromHeaders(headers):
  6.     '''從http響應(yīng)中獲取所有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
復(fù)制代碼

論壇徽章:
9
2015亞冠之阿爾納斯?fàn)?日期:2015-09-10 16:21:162015亞冠之塔什干火車頭
日期:2015-07-01 16:23:022015年亞洲杯之巴勒斯坦
日期:2015-04-20 17:19:46子鼠
日期:2014-11-13 09:51:26未羊
日期:2014-08-28 18:13:36技術(shù)圖書徽章
日期:2014-02-21 09:30:15酉雞
日期:2014-01-14 11:12:49天蝎座
日期:2013-12-09 17:56:53平安夜徽章
日期:2015-12-26 00:06:30
2 [報告]
發(fā)表于 2015-07-08 16:20 |只看該作者
學(xué)習(xí)了.......

論壇徽章:
6
CU大;照
日期:2013-03-13 15:15:08CU大;照
日期:2013-03-13 15:26:06CU大牛徽章
日期:2013-03-13 15:26:47戌狗
日期:2013-10-17 09:48:53CU十二周年紀(jì)念徽章
日期:2013-10-24 15:41:34丑牛
日期:2014-09-19 14:58:11
3 [報告]
發(fā)表于 2015-08-20 17:49 |只看該作者
不錯,要mark一下~~~~~~~~

論壇徽章:
0
4 [報告]
發(fā)表于 2015-12-28 14:18 |只看該作者
現(xiàn)在還好用么?
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP