使用request實現手工輸入驗證碼登錄


我們的目標網站是這個http://awehome.com.cn,登錄頁面是這個http://awehome.com.cn/tenant/login

import requests
import json

url = 'http://awehome.com.cn/tenant/login'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
    'Host': 'awehome.com.cn'
}
session = requests.session()
html = session.get(url=url,headers=headers)

搜索我們使用request的session來保存會話並且進入登錄頁面,他是這樣的

我們先來獲取驗證碼,直接通過html.text來獲取是找不到他里面的圖片的URL的。

<div class="form-group">
          <label for="LoginCaptcha">驗證碼</label>
          <div class="code-group02">
          <input type="text" class="form-control" name="Login[captcha]" 
                                                  v-model="login.captcha" 
                                                  id="LoginCaptcha" 
                                                  autocomplete="off" 
                                                  placeholder="驗證碼">
          <img class="captcha" v-on:click="onCaptcha" id="LoginCaptchaImg">
          </div>
        </div>

html.text這里面是沒有src的,他是渲染出來的, 所有我們去找這個js,發現他的函數就在login.js里面。

onCaptcha: function(e){
                var self = this;

                self.$http.get('/site/captcha?refresh=true').then(function(response){
                    $('#LoginCaptchaImg').attr('src', response.body.url);
                }, function(response){
                    alert(response.statusText);
                });

在login.js我們可以找到這個,可以發現他是通過ajax給/site/captcha?refresh=true發送獲取到數據然后將里面的response.body.url也就是驗證碼的URL設置給src標簽。

我們找一下site/captcha?refresh=true這個的包,就是‘http://awehome.com.cn/site/captcha?refresh=true’這個URL。他里面的內容是這樣

{"hash1":461,"hash2":461,"url":"/site/captcha?v=5c2735667c357"}

然后我們看到里面的URL,其實他就是驗證碼圖片的URL。既然我們找到圖片的URL就簡單了,將他保存下來然后手動查看輸入驗證碼即可。全部代碼在這

import requests
import json

url = 'http://awehome.com.cn/tenant/login'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
    'Host': 'awehome.com.cn'
}
session = requests.session()
html = session.get(url=url,headers=headers)
url1 = 'http://awehome.com.cn/site/captcha?refresh=true'
headers['Referer'] = url
html = session.get(url=url1,headers=headers)
img = json.loads(html.text)
url2 = 'http://awehome.com.cn'+ img['url']
img = session.get(url=url2)
with open('img.png','wb') as f:
    f.write(img.content)
img = input('請輸入驗證碼')
data = {
    'referer': 'http://awehome.com.cn/',
    'Login[dialcode]': '86',
    'Login[phone]': 'xxxxxxx',
    'Login[captcha]': str(img),
    'Login[password]': 'xxxx',
    'Login[rememberMe]': '0'
}
url='http://awehome.com.cn/tenant/login'
data_headers = {
'Accept': 'application/json, text/plain, */*',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Connection': 'keep-alive',
'Content-Length': '164',
'Content-Type': 'application/x-www-form-urlencoded',
'Host': 'awehome.com.cn',
'Origin': 'http://awehome.com.cn',
'Referer': 'http://awehome.com.cn/tenant/login',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest'

}
html = session.post(url=url,headers=data_headers,data=data)
url='http://awehome.com.cn/'
html = session.get(url=url,headers=headers)
print(html)
print(html.text)

 

大概步驟是先請求登錄頁面,然后通過抓包找到圖片URL,接着提交請求。

 


免責聲明!

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



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