# -*- coding: utf-8 -*- """ Created on Wed Jun 6 13:18:58 2018 @author: Lenovo """ # -*- coding: utf-8 -*- import requests import urllib import random from datetime import datetime # python2 和 python3的兼容代碼 try: # python2 中 import cookielib print(f"user cookielib in python2.") except: # python3 中 import http.cookiejar as cookielib print(f"user cookielib in python3.") # session代表某一次連接 huihuSession = requests.session() # 因為原始的session.cookies 沒有save()方法,所以需要用到cookielib中的方法LWPCookieJar,這個類實例化的cookie對象,就可以直接調用save方法。 huihuSession.cookies = cookielib.LWPCookieJar(filename = "huihuCookies.txt") userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36" header = { # "origin": "https://passport.huihu.cn", "Referer": "http://hh.haiper.com.cn/w/wander/user/login/", 'User-Agent': userAgent, } def huihuLogin(account, password): # print ("開始模擬登錄嘻嘻嘻") postUrl = "http://hh.haiper.com.cn/w/wander/user/login/" postData = { "username": account, "password": password, } # 使用session直接post請求 responseRes = huihuSession.post(postUrl, data = postData, headers = header) # 無論是否登錄成功,狀態碼一般都是 statusCode = 200 #responseRes = requests.post(postUrl, data = postData, headers = header) # 無論是否登錄成功,狀態碼一般都是 statusCode = 200 print(f"statusCode = {responseRes.status_code}") #print(f"text = {responseRes.text}") huihuSession.cookies.save() def isLoginStatus(): # 通過訪問個人中心頁面的返回狀態碼來判斷是否為登錄狀態 for i in range(2131,2134): routeUrl = "http://hh.haiper.com.cn/w/bench/extend/health/trade/all?nickname=&type=&gender=&level=&range%5Bstart%5D=2014-11-11+14%3A57&range%5Bend%5D=2018-06-06+14%3A57&page="+str(i) # 下面有兩個關鍵點 # 第一個是header,如果不設置,會返回500的錯誤 # 第二個是allow_redirects,如果不設置,session訪問時,服務器返回302, # 然后session會自動重定向到登錄頁面,獲取到登錄頁面之后,變成200的狀態碼 # allow_redirects = False 就是不允許重定向 try: responseRes = huihuSession.get(routeUrl, headers = header, allow_redirects = False) result = responseRes.text except: continue start = result.find('<div class="form-control-static form-control-static-list">') result = result[start:] #print (result) for j in range(1,16): start = result.find('擦擦擦圖片') if start==-1: break else: result = result[start:] start = result.find('src="') result = result[start+5:] end = result.find('" class="img-rounded"') imgpath = result[:end] print (imgpath) if imgpath=='/attachment/': continue randomname = datetime.now().strftime("%Y%m%d_%H%M%S") + str(random.randint(1,100))+'.jpg' try: urllib.request.urlretrieve(imgpath,'./擦擦擦/'+randomname) except: continue print (i) print(f"isLoginStatus = {responseRes.status_code}") #print(f"text = {responseRes.text}") if responseRes.status_code != 200: return False else: return True if __name__ == "__main__": # 從返回結果來看,有登錄成功 huihuLogin("xxxx", "xxxx") isLogin1 = isLoginStatus() print(f"is login huihu = {isLogin1}")