爬虫笔记9:session在突破12306图片验证中的作用

一、什么是session
session指的是一个会话。

二、需求描述
突破12306图片验证码(也就是验证成功)

三、策略
账号正确 密码错误 验证码错误 {“result_message”:“验证码校验失败”,“result_code”:“5”}
账号正确 密码错误 验证码正确 {“result_message”:“验证码校验成功”,“result_code”:“4”}
账号正确 密码正确 验证码正确 OK
根据上面3次的尝试,得到结论:12306是先验证成功后,才会去看账号和密码是否正确

四、步骤
1、 明确目标url,发送post请求 ,并携带数据
爬虫笔记9:session在突破12306图片验证中的作用_第1张图片
(1)先在登录页面点击右键检查,然后我们输入错误账户、错误密码、正确的验证码,点击登录;
(2)点击network,在name中找到Response显示的是‘验证码校验成功’,对应的headers中的url就是我们的目标url:
(3)在headers中下方的Query String Parameters中找到要携带的数据:
answer: 46,45,38,112
rand: sjrand
login_site: E
爬虫笔记9:session在突破12306图片验证中的作用_第2张图片
(补充:从携带数据可以看出,这实际上是一个坐标验证。)
2 、获取12306的图片验证码
鼠标放在验证图片上,点击右键–在新标签页中打开图片
爬虫笔记9:session在突破12306图片验证中的作用_第3张图片
得到:
爬虫笔记9:session在突破12306图片验证中的作用_第4张图片
从它的url可以看出,这个地址是经过base64伪加密的。
base64伪加密是用64个字符来表示任意的二进制数据的方法。( A-Z A-Z 0 - 9 + / 这64个字符)
复制这个url,并通过下面的得到,得到验证图片。

import base64

url ='/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAC+ASUDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFD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JT/AP7ZXttFTyR7G31us/tfkeOaX8B1sNTt7uXxK8qQvv2JZhCSOnJc98HofSurn+HKTxXcP9pOIbiF4gCJGILKVyQZNpHOcBRyBXcUVnPDUpu8l+YfWq23MeeX/wAONRvkhB1nTFaAbYZG0fzmiUHgL5krKOg5xk4BzmuYP7PVs0D7/EcpuDna62gCLypHy7+3z9/4h6c+1UVUKMIfCifb1O55hB8FtKtZxJDNbqBZvCFNu5/esADIT5vQfNhf9rrwKhsfhDqOmWtpDYeKktZLaJoVuIrBvMZWk8xgczFfvdwoOABk4r1WitOVE+1n3CiiimZnAHAjACElKFABMW0KEX4jJbIA/9kKCg=='

img_data = base64.b64decode(url) # 返回的是二进制数据
print(type(img_data))
fn = open('code.png','wb')
fn.write(img_data)
fn.close()

结果:
爬虫笔记9:session在突破12306图片验证中的作用_第5张图片
3 、点击正确的图片

import requests

def login():
    # 获取验证码图片
    pic_response = requests.get('https://kyfw.12306.cn/passport/captcha/captcha-image?login_site=E&module=login&rand=sjrand')
    codeImage = pic_response.content
    fn = open('code2.png','wb')
    fn.write(codeImage)
    fn.close()
    # 从验证码图片的左上角为 (0,0),截图看正确答案的坐标,并在input要求输入时输入
    codeStr = input('请输入验证码坐标:')

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36'
    }
    data = {
        'answer': codeStr,
        'rand': 'sjrand',
        'login_site': 'E'
    }

    response =requests.post('https://kyfw.12306.cn/passport/captcha/captcha-check',data=data,headers=headers)

    print(response.text)

login()

爬虫笔记9:session在突破12306图片验证中的作用_第6张图片
189,50就是我们要在input时输入的。
结果:
爬虫笔记9:session在突破12306图片验证中的作用_第7张图片
仍然提示不对,这时就用到session了,要保持会话的状态。
语法:req = requests.session()

import requests
req = requests.session() # 保持会话

def login():
    # 获取验证码图片
    pic_response = req.get('https://kyfw.12306.cn/passport/captcha/captcha-image?login_site=E&module=login&rand=sjrand')
    codeImage = pic_response.content
    fn = open('code2.png','wb')
    fn.write(codeImage)
    fn.close()
    # 从验证码图片的左上角为 (0,0),截图看正确答案的坐标,并在input要求输入时输入
    codeStr = input('请输入验证码坐标:')

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36'
    }
    data = {
        'answer': codeStr,
        'rand': 'sjrand',
        'login_site': 'E'
    }

    response = req.post('https://kyfw.12306.cn/passport/captcha/captcha-check',data=data,headers=headers)

    print(response.text)

login()

补充:当出现要点击多个图片验证时,如下输入:
爬虫笔记9:session在突破12306图片验证中的作用_第8张图片

你可能感兴趣的:(爬虫笔记9:session在突破12306图片验证中的作用)