登录验证码识别

2023-12-11  本文已影响0人  mylemon
from lxml import etree
import requests
import tujian
headers = {
   'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
}
#将验证码图片请求后保存到本地
login_url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
page_text = requests.get(url=login_url,headers=headers).text
tree = etree.HTML(page_text)
img_src = 'https://so.gushiwen.cn'+tree.xpath('//*[@id="imgCode"]/@src')[0]
code_data = requests.get(url=img_src,headers=headers).content
with open('./code.jpg','wb') as fp:
   fp.write(code_data)

#识别验证码图片内容
result = tujian.getImgCodeText('./code.jpg',3)
print(result)

模拟登录

from lxml import etree
import requests
import tujian
headers = {
    'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
}
#将验证码图片请求后保存到本地
login_url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
page_text = requests.get(url=login_url,headers=headers).text
tree = etree.HTML(page_text)
img_src = 'https://so.gushiwen.cn'+tree.xpath('//*[@id="imgCode"]/@src')[0]
code_data = requests.get(url=img_src,headers=headers).content
with open('./code.jpg','wb') as fp:
    fp.write(code_data)

#识别验证码图片内容
result = tujian.getImgCodeText('./code.jpg',3)
print(result)
#模拟登录
url = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx'
data = {
    "__VIEWSTATE": "opfVI7oolwkr7MLRVzsNSMASqLRUuO1dg5ZP5EIRa4FyM+mOYKEs6KWEKQKaba2ulLoZQIaLFiKK4mr5K3ci1v8ua28wtcRtabKWjOtJtU/i2etH+zSduegTMcg=",
    "__VIEWSTATEGENERATOR": "C93BE1AE",
    "from": "http://so.gushiwen.cn/user/collect.aspx",
    "email": "xxxx",
    "pwd": "xxxx",
    "code":result ,
    "denglu": "登录"
}
#获取了登录成功后的页面源码数据
login_page_text = requests.post(url=url,headers=headers,data=data).text
with open('wushiwen.html','w') as fp:
    fp.write(login_page_text)
*   查看gushiwen.html发现,没有登录成功,原因:

    *   验证码不对(否定)

    *   没有携带cookie

    *   出现了动态变化的请求参数

        *   如何获取动态变化的请求参数
            *   基于抓包工具进行全局搜索,发现该参数值被隐藏在了登录页面的页面源码中
from lxml import etree
import requests
import tujian
headers = {
    'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
}
#创建session对象
session = requests.Session()

#将验证码图片请求后保存到本地
login_url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
page_text = session.get(url=login_url,headers=headers).text
tree = etree.HTML(page_text)
img_src = 'https://so.gushiwen.cn'+tree.xpath('//*[@id="imgCode"]/@src')[0]
code_data = session.get(url=img_src,headers=headers).content
with open('./code.jpg','wb') as fp:
    fp.write(code_data)

#解析出动态变化的请求参数
__VIEWSTATE = tree.xpath('//*[@id="__VIEWSTATE"]/@value')[0]

#识别验证码图片内容
result = tujian.getImgCodeText('./code.jpg',3)
print(result)
#模拟登录
url = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx'
data = {
    "__VIEWSTATE": __VIEWSTATE,
    "__VIEWSTATEGENERATOR": "C93BE1AE",
    "from": "http://so.gushiwen.cn/user/collect.aspx",
    "email": "xxxx",
    "pwd": "xxxx",
    "code":result ,
    "denglu": "登录"
}
#获取了登录成功后的页面源码数据
login_page_text = session.post(url=url,headers=headers,data=data).text
with open('wushiwen.html','w') as fp:
    fp.write(login_page_text)

上一篇下一篇

猜你喜欢

热点阅读