• Python模拟知乎登录


    # -*- coding:utf-8 -*-
    import urllib
    import urllib2
    import cookielib
    import time
    from PIL import Image
    
    cookie = cookielib.CookieJar()
    handler = urllib2.HTTPCookieProcessor(cookie)
    opener = urllib2.build_opener(handler)
    urllib2.install_opener(opener)
    url = 'https://www.zhihu.com'
    post_url = 'https://www.zhihu.com/login/phone_num'
    agent = 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'
    respose = urllib2.urlopen(url)
    
    #获取验证码并存储
    def saveImg():
        t = str(int(time.time() * 1000))
        captcha_url = 'https://www.zhihu.com/captcha.gif?r=' + t + "&type=login&lang=cn"
        captcha = urllib2.urlopen(captcha_url)
        data =captcha.read()
        with open('captcha.gif', 'wb') as f:
            f.write(data)
        img = Image.open('captcha.gif')
        img.show()
    
    #获取验证码中倒立文字的位置
    def getPoints(n):
        switch = {'1':[20.375,22],'2':[45.375,23],'3':[70.375,21],'4':[95.375,20],'5':[120.375,18],'6':[145.375,29],'7':[170.375,17]}
        ls = []
        for i in list(n):
            ls.append(switch[i])
        return ls
    
    saveImg()
    num = raw_input('请输入验证码:')
    ls_points = str(getPoints(num))
    #获取_xsrf
    for item in cookie:
        if item.name == '_xsrf':
            _xsrf = item.value
    headers = {
       'User-Agent':agent,
        'Referer':'https://www.zhihu.com/',
        'X-Xsrftoken':_xsrf
    }
    postdata = {
       '_xsrf': _xsrf,
       'password': '******',
        'captcha': '{"img_size":[200,44],"input_points":'+ls_points+'}',
       'captcha_type': 'cn',
       'phone_num': '******'
    }
    data = urllib.urlencode(postdata)
    request = urllib2.Request(post_url, data, headers)
    result = urllib2.urlopen(request)
    print result.read()
    

      

  • 相关阅读:
    在桌面上显示IE图标(非快捷键)
    在桌面上显示IE图标(非快捷键)
    Detours的使用准备
    Detours的使用准备
    腾讯机试题 AcWing 603 打怪兽
    牛客练习赛13D 幸运数字4
    牛客练习赛13B 幸运数字2
    牛客练习赛13E 乌龟跑步
    NOIP2016提高组复赛C 愤怒的小鸟
    Leetcode SingleNumber I & II & III 136/137/260
  • 原文地址:https://www.cnblogs.com/bbbbbd/p/7833940.html
Copyright © 2020-2023  润新知