Python 模擬驗證碼登陸


Python 模擬驗證碼登陸

  • 獲取登錄請求
    • 打開preserve log
    • 點擊登錄,獲取登錄請求(post)
  • 驗證碼地址可變
    • 爬取頁面驗證碼地址,獲取驗證碼內容
    • 將data進行post請求
  • 驗證碼地址不變,而內容隨機變化
    • 設置session進行驗證碼的get請求並下載圖片進行識別得到驗證碼的識別結果,再利用這個sesson進行post請求,把賬號密碼和驗證碼識別結果的表單數據進行post從而模擬登錄

      • 圖
      • 如果請求中產生了cookie,則該cookie會被自動存儲/攜帶在該session對象中
      • session可以進行請求的發送
    sesson =requests.session()
    code_img_data = sesson.get(url=url,headers=headers).content
    #
    #
    #
    response = sesson.post(url = log_url,headers = headers,data=data)

模擬古詩詞網登錄
codet.py:

#!/usr/bin/env python
# coding:utf-8

import requests
from hashlib import md5

class Chaojiying_Client(object):

    def __init__(self, username, password, soft_id):
        self.username = username
        password =  password.encode('utf8')
        self.password = md5(password).hexdigest()
        self.soft_id = soft_id
        self.base_params = {
            'user': self.username,
            'pass2': self.password,
            'softid': self.soft_id,
        }
        self.headers = {
            'Connection': 'Keep-Alive',
            'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
        }

    def PostPic(self, im, codetype):
        """
        im: 圖片字節
        codetype: 題目類型 參考 http://www.chaojiying.com/price.html
        """
        params = {
            'codetype': codetype,
        }
        params.update(self.base_params)
        files = {'userfile': ('ccc.jpg', im)}
        r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers)
        return r.json()

    def ReportError(self, im_id):
        """
        im_id:報錯題目的圖片ID
        """
        params = {
            'id': im_id,
        }
        params.update(self.base_params)
        r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
        return r.json()

#
# if __name__ == '__main__':
#     chaojiying = Chaojiying_Client('超級鷹用戶名', '超級鷹用戶名的密碼', '96001')	#用戶中心>>軟件ID 生成一個替換 96001
#     im = open('a.jpg', 'rb').read()													#本地圖片文件路徑 來替換 a.jpg 有時WIN系統須要//
#     print chaojiying.PostPic(im, 1902)												#1902 驗證碼類型  官方網站>>價格體系 3.4+版 print 后要加()

main.py:

from codet import Chaojiying_Client
import requests
from lxml import etree
if __name__ == '__main__':
   headers = {
       "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"
               }
   url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
   #利用session保存cookie信息
   sesson =requests.session()
   #登錄界面
   log_page_text = sesson.get(url=url,headers=headers).text
   tree = etree.HTML(log_page_text)
   #驗證碼圖片鏈接
   code_img_url = 'https://so.gushiwen.cn'+tree.xpath('//img[@id="imgCode"]/@src')[0]
   print(code_img_url)
   #獲取驗證碼內容,每次請求驗證碼內容都會更改,利用session保存最后一次更新的cookie
   code_img_data = sesson.get(code_img_url).content
   with open('code.jpg','wb') as fp:
       fp.write(code_img_data)

   chaojiying = Chaojiying_Client('wi0i0i', '********', '920413')	#用戶中心>>軟件ID 生成一個替換 96001
   im = open('code.jpg', 'rb').read()													#本地圖片文件路徑 來替換 a.jpg 有時WIN系統須要//
   #print (chaojiying.PostPic(im, 1902)	)
   pic_str = chaojiying.PostPic(im, 1902)['pic_str']
   print(pic_str)
   #點擊登錄按鈕,產生的請求
   headers = {
       'referer': 'https: // so.gushiwen.cn / user / login.aspx?from=http: // so.gushiwen.cn / user / collect.aspx',
       "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"
   }
   log_url = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx'
   data = {
       '__VIEWSTATE': 'AVP2UqKeeCzJep4Lb05tbFRvGOQjFCxVHE / EFBHpF + W + +3eo2R8m6hLLw + FLJNXGm1wVguwWRixwR3U84ig2ifnQJyjcBkLZdLzBWvC7t5XiZvnXcLnVaofhEtk =',
   '__VIEWSTATEGENERATOR': 'C93BE1AE',
   'from': 'http://so.gushiwen.cn/user/collect.aspx',
   'email': '3059519959@qq.com',
   'pwd': '*********',
   'code': pic_str,
   'denglu': '登錄'
   }
   #利用session保存的cookie,保持會話,保證驗證碼內容一致,
   response = sesson.post(url = log_url,headers = headers,data=data)
   print(response.status_code)
   with open('log.html','w',encoding='utf-8') as fp:
       fp.write(response.text)
  • 代理
    • 請求參數- proxies = {"http":"0.0.0.0:2222"}


免責聲明!

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



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