自動簽到腳本


從來沒有接觸過Python,昨天為了實現自動簽到,在c74的帶領下,找到一個人人的自動簽到腳本,於是照葫蘆畫瓢,改成班級網站的自動簽到。

人人:

import urllib.request

class renren:

    def __init__(self):
        self.operate=''
        try:
            self.opener=urllib.request.build_opener(urllib.request.HTTPCookieProcessor())
            urllib.request.install_opener(self.opener)
        except Exception as e:
            print(str(e))
        
    def login(self,email,pwd):
        print('login.....')
        user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
        headers = { 'User-Agent' : user_agent}
        params={'domain':'renren.com','origURL':'http://www.renren.com/indexcon','email':email,'password':pwd}
        params=urllib.parse.urlencode(params)
        params=params.encode('ISO-8859-1')
        req=urllib.request.Request('http://www.renren.com/PLogin.do',params,headers)
        self.operate=self.opener.open(req)
        print(self.operate.geturl())#這里可以根據返回的url判斷是否登錄成功
   


email=''#登錄郵箱
pwd=''#密碼

rr=renren()
data=rr.login(email,pwd)
View Code

 

模擬報文,可以用chrome去查看原來的報文長什么樣。在登陸的時候看看發送了什么東西給服務器,一般是一個POST報文或GET報文。

import urllib.request
import urllib.parse
import re
class renren:

    def __init__(self):
        self.operate=''
        try:
            self.opener=urllib.request.build_opener(urllib.request.HTTPCookieProcessor())
            urllib.request.install_opener(self.opener)
        except Exception as e:
            print(str(e))
        
    def login(self,login,username,passward):
        print('login.....')
        user_agent = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22'
        headers = { 'User-Agent' : user_agent}
        params={'login':login,'username':username,'password':password}
        params=urllib.parse.urlencode(params)
        params=params.encode('utf8')
        req=urllib.request.Request('http://sysucs.org/login.php',params,headers)
        self.operate=self.opener.open(req)
        print(self.operate.geturl())#這里可以根據返回的url判斷是否登錄成功
        
        while True:
            fp = urllib.request.urlopen('http://sysucs.org/mission.php')
            mystr = fp.info()
            #mystr = mybytes.decode("utf8")
            print(mystr)
        
username=''#登錄郵箱
password=''#密碼
login = '0'

rr=renren()
data=rr.login(login,username,password)
View Code

 

后來覺得不妥,總是這樣循環請求會增加服務器的負擔,況且大班長已經加入了檢測循環請求的功能,限制了每秒訪問頁面的次數。

所以想起計網中說的,報文。雖然在班級網站上屏蔽了時間,但是你模擬一個報文給服務器的時候,服務器就會返回一個響應報文,響應報文里面就會有服務器時間的啦。

然后呢,從服務器返回的相應報文中提取時間。查了下Python文檔,從報文類型里面查找時間的字符串,再進行處理就可以得到服務器時間啦。

接着調用一個睡眠函數,讓程序睡夠一定的時間再去請求簽到頁面。這樣就能實現定時腳本簽到。

因為有網絡延遲,所以簽到時間會00:00:00-00:00:01秒之間,網絡太差的話還可能超過1秒。

然后寫成一個bat,再利用win7的定時計划功能,每天晚上差不多12點的時候,自動運行這個bat.

只要在12點前的1、2分鍾電腦是開的並且是聯網的,就能自動簽到了。

 

import urllib.request
import urllib.parse
import time
import string
class renren:

    def __init__(self):
        self.operate=''
        try:
            self.opener=urllib.request.build_opener(urllib.request.HTTPCookieProcessor())
            urllib.request.install_opener(self.opener)
        except Exception as e:
            print(str(e))
        
    def login(self,login,username,passward):
        print('login.....')
        user_agent = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22'
        headers = { 'User-Agent' : user_agent}
        params={'login':login,'username':username,'password':password}
        params=urllib.parse.urlencode(params)
        params=params.encode('utf8')
        req=urllib.request.Request('http://www.sysucs.org/login.php',params,headers)
        self.operate=self.opener.open(req)
        print(self.operate.geturl())#這里可以根據返回的url判斷是否登錄成功
        
        #while True:
        fp = urllib.request.urlopen('http://www.sysucs.org/index.php')#return http.client.HTTPResponse
        mystr = fp.info()
        date = mystr.get("Date")
        index = date.find("2013 ")
        times = date[index+5:index+13]
        print(times)
        mins = times[3:5]
        secs = times[6:8]
        count = 60-int(secs)+(59-int(mins))*60
        print(count)
        time.sleep(count)
        fp2 = urllib.request.urlopen('http://www.sysucs.org/mission.php')#return http.client.HTTPResponse
        print("End")
username=''#登錄郵箱
password=''#密碼
login = '0'

rr=renren()
data=rr.login(login,username,password)
View Code

 

其實要是課內的東西能找到實踐的平台,而不僅僅停留在考試上,那么計算機系出來的學生一定牛逼轟轟。

可惜絕大多數知識總是停留在考試理論層面。說實話,從小到大,已經厭倦了。

書扔掉過2天你就什么都不會了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM