In the future, more anti-crawler articles will be published.

I found a slider verification code when I logged in to a certain website

Making the address

So let’s look at this slider

Enter debug mode (press F12) and capture the interface and the following parameters

This is the validation interface that returns the key login parameter validate

Next we take the verification image, process and calculate the gap to the left

Before running the code, we install the required packages.

-m PIP install –upgrade PIP # Modified to pip3 installation cv2 [PIP install -i pypi.tuna.tsinghua.edu.cn/simple opencv – python] (www.jianshu.com/p/33bbaadd6)… Install the selenium PIP install -i pypi.tuna.tsinghua.edu.cn/simple selenium to install numpy PIP install – I pypi.tuna.tsinghua.edu.cn/simple numpy

# # # #!!!!! If you run the selenium error, may be no download chrom driver download address: npm.taobao.org/mirrors/chr… Note that you need to download your own Version of Google Browser, otherwise an error will be reported.

#### Put the code next

# coding=utf-8
# !/usr/bin python
"' the Author: [MaNongXiaoGang] (https://www.jianshu.com/u/2f19d18204ce) Python3 environment ' ' '
import json
import re
import cv2
import base64
import requests
import numpy as np
from selenium import webdriver

from selenium.webdriver.chrome.options import Options

chrome_options = Options()
# Set Chrome to no interface mode
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(chrome_options=chrome_options)


def decode_image(src, filename):
    # 1. Information extraction
    result = re.search("data:image/(? P
      
       .*?) ; base64,(? P
       .*)"
      , src, re.DOTALL)
    if result:
        ext = result.groupdict().get("ext")
        data = result.groupdict().get("data")
    else:
        raise Exception("Do not parse!")
    img = base64.urlsafe_b64decode(data)
    with open(filename, "wb") as f:
        f.write(img)
    return img


def get_image_position(a):
    driver.get('https://www.luonet.com/login')
    cookies = driver.get_cookies()
    deviceId = list(filter(lambda key: 'deviceId' == key['name'], cookies))[0]
    ua = 'the Mozilla / 5.0 (Macintosh; Intel Mac OS X 10.12; The rv: 56.0) Gecko / 20100101 Firefox 56.0 / '
    img_url = 'https://www.luonet.com/dcapi/captcha'
    s = requests.session()

    for cookie in cookies:
        s.cookies.set(cookie['name'], cookie['value'])
    req = s.get(img_url, headers={'User-Agent': ua})
    jso = req.json()
    image1 = jso['bg']
    image2 = jso['front']
    decode_image(image2, 'slide_bkg.png')
    decode_image(image1, 'slide_block.png')

    block = cv2.imread('slide_block.png'.0)
    template = cv2.imread('slide_bkg.png'.0)

    cv2.imwrite('template.jpg', template)
    cv2.imwrite('block.jpg', block)
    block = cv2.imread('block.jpg')

    block_cvt = cv2.cvtColor(block, cv2.COLOR_BGR2GRAY)  
    block_cvt = abs(255 - block_cvt)
    cv2.imwrite('block.jpg', block_cvt)

    block_cvt = cv2.imread('block.jpg')
    template = cv2.imread('template.jpg')

    result = cv2.matchTemplate(block_cvt, template, cv2.TM_CCOEFF_NORMED)
    x, y = np.unravel_index(result.argmax(), result.shape)
    print(x, y)
    data = {
        'offsetLeft': y + 0.132941176470587.'offsetTop': x
    }
    url = 'https://www.luonet.com/dcapi/captcha/verify'

    res = s.post(url=url, headers={'User-Agent': ua}, data=data)
    jso = res.json()
    if jso['success']:
        validate = jso['validate']
        payload = {
            "username": "".# account
            "password": "".# your password
            "validate": validate,
            "id": deviceId['value']
        }
        login_url = 'https://www.luonet.com/dcapi/oauth/token'
        headers = {
            'Accept': 'application/json, text/plain, */*'.'Accept-Encoding': 'gzip, deflate, br'.'Accept-Language': 'zh-CN,zh; Q = 0.9 '.'Cache-Control': 'no-cache'.'Connection': 'keep-alive'.'Content-Type': 'application/json; charset=UTF-8'.'deviceId': deviceId['value'].'Host': 'www.luonet.com'.'If-Modified-Since': 'Thu, 01 Jun 1970 00:00:00 GMT'.'Origin': 'https://www.luonet.com'.'Pragma': 'no-cache'.'Referer': 'https://www.luonet.com/login'.'Sec-Fetch-Dest': 'empty'.'Sec-Fetch-Mode': 'cors'.'Sec-Fetch-Site': 'same-origin'.'shouldIntercept': 'true'.'User-Agent': 'the Mozilla / 5.0 (Windows NT 10.0; Win64; X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'.'X-Requested-With': 'XMLHttpRequest'
        }

        res = s.post(url=login_url, headers=headers, data=json.dumps(payload))
        print(res.json())
        res.json()


if __name__ == '__main__':
    get_image_position()

Copy the code

Running Result Screenshot

This article is intended for academic exchange and is not intended for commercial use