首页 > 其他 > 详细

验证码操作

时间:2018-09-27 23:25:56      阅读:268      评论:0      收藏:0      [点我收藏+]

 

 

#下载验证码到本地
import requests
from lxml import etree
import urllib.request

s = requests.session()

url = https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx
headers = {
    user-agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
}

page_content = s.get(url,headers=headers).text

tree = etree.HTML(page_content)
img_url = tree.xpath(//img[@id="imgCode"]/@src)[0]
img_url = https://so.gushiwen.org + img_url

#urllib.request.urlretrieve(img_url,‘./code.png‘)
img_data = s.get(url=img_url,headers=headers).content
with open(./code.png,wb) as fp:
    fp.write(img_data) 

#解析form表单的令牌数据(反爬手段)
key_1 = tree.xpath(//*[@id="__VIEWSTATE"]/@value)[0]
key_2 = tree.xpath(//*[@id="__VIEWSTATEGENERATOR"]/@value)[0]

#step 2:抓包获取登录url
code = input(查看下验证码内容:)
post_url = https://so.gushiwen.org/user/login.aspx?from=http%3a%2f%2fso.gushiwen.org%2fuser%2fcollect.aspx
data = {
    "__VIEWSTATE":key_1,
    "__VIEWSTATEGENERATOR":key_2,
    "from":"http://so.gushiwen.org/user/collect.aspx",
    "email":"www.zhangbowudi@qq.com",
    "pwd":"123456",
    "code":code,
    "denglu":"登录"
}
page_content = s.post(url=post_url,headers=headers,data=data).text

with open(./second.html,w,encoding=utf-8) as fp:
    fp.write(page_content)
print(over)

 

验证码操作

原文:https://www.cnblogs.com/xujinjin18/p/9716146.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!