zoukankan      html  css  js  c++  java
  • 验证码操作

    #下载验证码到本地
    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')
  • 相关阅读:
    【Cocos2d-x for WP8 学习整理】(1)创建一个新项目
    与本机其他应用交互的简单介绍
    Windows Phone GB2312
    Windows Phone 横竖屏切换动画
    Linq To VisualTree
    Epub基础知识介绍
    【使用Unity开发Windows Phone上的2D游戏】(2)初识工具
    操作系统Unix、Windows、Mac OS、Linux的故事
    PHP开发环境搭建
    linux和mac
  • 原文地址:https://www.cnblogs.com/xujinjin18/p/9716146.html
Copyright © 2011-2022 走看看