python爬取学籍_python 爬取现充Shell的成绩单
以前一直都是照著別人的爬蟲進行修改實驗,這次自己親手查請求包看源碼完成了一個爬蟲。寫篇blog紀念一下(
具體需求是@ShellBin想要幫女朋友查成績,但是他女朋友不記得她自己的學籍號了,查成績需要身份證號和學籍號,缺一不可,于是,他想要爆破查成績。
這次使用的是python3和requests包
首先,我們要通過抓包獲取請求的格式,我們嘗試輸入身份證和學籍號,通過chrome的抓包我們可以看到請求頭、請求地址和請求正文的格式。
當然,因為我們隨便發送了一個身份證和學籍號,所以返回的請求是一個輸入錯誤。
有了請求的頭、地址和正文,我們可以嘗試拼裝出一個請求,嘗試使用現充shell本人的信息進行查詢。
xjh = '**************'
sfzh = '******************'
params = {'sfzh': sfzh, 'xjh': xjh}
url = 'http://218.26.172.230/2011/Home/Query'
r = requests.post(url,data = params)
我們收到了一個json格式的數據包,同樣,我們使用r.text將數據取出來就可以看到現充的丑惡嘴臉了(
接下來就是如何遍歷學籍號了,現充shell告訴我學籍號由兩個部分組成,一段前綴和0000-9999的尾數。所以我們需要一個leftpad(不是在黑node.js)
def zeropad(num,lenth):
dlen = lenth-len(str(num))
temps = ''
for x in range(dlen):
temps += '0'
return temps + str(num)
然后我們只需設定好現充shell女友的身份證號后暴力遍歷即可,由于數據是json格式,我們需要引入json包。
for x in range(10000):
xjh = '1401141702'+ zeropad(x,4)
sfzh = '******************'
params = {'sfzh': sfzh, 'xjh': xjh}
url = 'http://218.26.172.230/2011/Home/Query'
r = requests.post(url,data = params)
jsondata = json.loads(r.text)
if('error' not in jsondata.keys()):
print(r.text)
然后。我們發現由于數據量過大,而整個程序的瓶頸在于服務器交互上,所以我決定使用多線程來優化程序。
我們引入threading包和queue包。threading包用于開線程,queue包用于為線程分發任務。
這同時,我們會遇到幾個問題:
在找到正確的學籍號后,我們怎樣終止所有的線程?
如何為線程分發任務使之不沖突?
經過一番查找,我決定使用人為拋出異常來終止線程。
而分發任務上我決定通過queue來進行分發(當然機智的現充shell也想出了一個好辦法,將整個0000-9999的數據平均分為20份,直接分配給20個線程)
人為拋出異常的實現
if(flag):
raise SystemError("i'm just find it")
分發任務的實現
flag=0
task_queue = queue.Queue()
def checkid():
x = task_queue.get()
global flag
if(flag):
raise SystemError("i'm just find it")
xjh = '1401141702'+ zeropad(x,4)
sfzh = '******************'
params = {'sfzh': sfzh, 'xjh': xjh}
url = 'http://218.26.172.230/2011/Home/Query'
r = requests.post(url,data = params)
jsondata = json.loads(r.text)
if('error' not in jsondata.keys()):
print(r.text)
flag=1
checkid()
這樣,我們只要開啟多個線程來執行checkid函數即可
for x in range(20):
sthread = threading.Thread(target=checkid)
sthread.start()
效率確實得到了明顯的提升。查找速度提升了大概五六倍。
但是還有個問題,由于我們拋出了異常并且開了20個線程,最后的異常顯示就會把我們的屏幕堆滿,不知道最終輸出在哪里了。所以我們引入sys包,重定向異常的輸出位置。并使用try…catch…來捕獲異常,將異常輸出關掉。
sys.stderr = None
這樣,整個結果就很明朗了,全部代碼如下:
import requests
import json
import threading
import queue
import sys
sys.stderr = None
flag=0
task_queue = queue.Queue()
def zeropad(num,lenth):
dlen = lenth-len(str(num))
temps = ''
for x in range(dlen):
temps += '0'
return temps + str(num)
def checkid():
try:
x = task_queue.get()
global flag
if(flag):
raise SystemError("i'm just find it")
xjh = '1401141702'+ zeropad(x,4)
sfzh = '******************'
params = {'sfzh': sfzh, 'xjh': xjh}
url = 'http://218.26.172.230/2011/Home/Query'
r = requests.post(url,data = params)
jsondata = json.loads(r.text)
if('error' not in jsondata.keys()):
print(r.text)
flag=1
checkid()
except Queue.Empty:
pass
for x in range(10000):
task_queue.put(x)
for x in range(20):
sthread = threading.Thread(target=checkid)
sthread.start()
參考資料:
總結
以上是生活随笔為你收集整理的python爬取学籍_python 爬取现充Shell的成绩单的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: anguarjs 上传图片预览_设计神器
- 下一篇: python3 isinstance用法