首页 > 编程语言 >python 实现 短信登录 b站 并打印cookie内容

python 实现 短信登录 b站 并打印cookie内容

时间:2023-02-20 17:00:29浏览次数:62  
标签:并打印 python country json cname cookie print post id

前言

本程序是get_bili_medal_list项目
的一个子程序,用于大批量获取用户数据时的cookie替换。

使用

项目根目录运行python login_get_cookie.py,根据提示完成短信验证登录。
期间需要访问http://geetest.colter.top/,传入gtchallenge完成手动的极验校验,获取validateseccode
最后需要填入手机验证码完成登录即可。
ps:python login_get_cookie.py 1,优化了validateseccode的输入,请自行选择执行。

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

源码

login_get_cookie.py

依赖的第三方库,自行补装一下

import json
import asyncio
import aiohttp
import sys, re
from itertools import islice

header1 = {
    'content-type': 'application/x-www-form-urlencoded',
    "referer": "https://space.bilibili.com",
    "Origin": "https://space.bilibili.com",
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Core/1.94.186.400 '
}

proxys = None

# 国家id转对应的cid
async def get_cid(country_id):
    with open("data/country.json", "r", encoding="utf8") as f:
        country_data = json.load(f)
    
    for common in country_data["data"]["common"]:
        if common["country_id"] == country_id:
            return common["id"]
    
    return 1

# 获取验证码相关信息
async def get_check():
    API_URL = 'https://passport.bilibili.com/x/passport-login/captcha?source=main_web'
    async with aiohttp.ClientSession(headers=header1) as session:
        try:
            async with session.get(url=API_URL, headers=header1, proxy=proxys) as response:
                if response.status != 200:
                    response.raise_for_status()
                ret = await response.json()
        except aiohttp.ClientError as e:
            print(e)
            return {"code": -1}
            
    return ret


# 获取captcha_key喵
async def get_captcha_key(post_data):
    API_URL = 'https://passport.bilibili.com/x/passport-login/web/sms/send'
    async with aiohttp.ClientSession(headers=header1) as session:
        try:
            async with session.post(url=API_URL, headers=header1, proxy=proxys, data=post_data) as response:
                if response.status != 200:
                    response.raise_for_status()
                ret = await response.json()
        except aiohttp.ClientError as e:
            print(e)
            return {"code": -400}
            
    return ret


# 登录
async def login(post_data):
    API_URL = 'https://passport.bilibili.com/x/passport-login/web/login/sms'
    async with aiohttp.ClientSession(headers=header1) as session:
        try:
            async with session.post(url=API_URL, headers=header1, proxy=proxys, data=post_data) as response:
                if response.status != 200:
                    response.raise_for_status()

                print("\n返回的cookie如下")
                print(response.cookies)
                
                ret = await response.json()
        except aiohttp.ClientError as e:
            print(e)
            return {"code": -400}
            
    return ret

async def main():
    banner = r"""
            \\         //
            \\       //
        #####################     ________   ___   ___        ___   ________   ___   ___        ___
        ##                 ##    |\   __  \ |\  \ |\  \      |\  \ |\   __  \ |\  \ |\  \      |\  \
        ##    //     \\    ##    \ \  \|\ /_\ \  \\ \  \     \ \  \\ \  \|\ /_\ \  \\ \  \     \ \  \
        ##   //       \\   ##     \ \   __  \\ \  \\ \  \     \ \  \\ \   __  \\ \  \\ \  \     \ \  \
        ##                 ##      \ \  \|\  \\ \  \\ \  \____ \ \  \\ \  \|\  \\ \  \\ \  \____ \ \  \
        ##       www       ##       \ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\
        ##                 ##        \|_______| \|__| \|_______| \|__| \|_______| \|__| \|_______| \|__|
        #####################
            \/         \/                               哔哩哔哩 (゜-゜)つロ 干杯~
    """

    # 获取captcha_key的传参
    post_data1 = {
        "cid": "86",
        "tel" : "0",
        "source" : "main-fe-header",
        "token" : "",
        "challenge" : "",
        "validate" : "",
        "seccode" : ""
    }
    # 登录传参
    post_data2 = {
        "cid": "86",
        "tel" : "0",
        "code" : 0,
        "source" : "main-fe-header",
        "captcha_key" : ""
    }

    print(banner + "\n")

    # 获取命令行传入的参数
    args = sys.argv

    # 输出命令行传入的参数
    # print(args)

    country_id = input("请输入手机的国家代码(不填默认 86):")
    tel = input("请输入手机号码:")

    if country_id == "":
        # post_data1["cid"] = 1
        country_id = "86"

    # 获取下cid
    # post_data1["cid"] = await get_cid(country_id)
    post_data1["cid"] = country_id

    if tel == "":
        print("输入手机号啊!kora!")
        return
    else:
        post_data1["tel"] = tel

    print("[开始获取验证信息]")
    token_json = await get_check()
    print(json.dumps(token_json, indent=2, ensure_ascii=False))

    if token_json["code"] != 0:
        print("获取校验信息失败,取名为寄喵~")
        return

    try:
        post_data1["token"] = token_json["data"]["token"]
        gt = post_data1["challenge"] = token_json["data"]["geetest"]["gt"]
        post_data1["challenge"] = token_json["data"]["geetest"]["challenge"]

        print("\n请访问:http://geetest.colter.top/ 继续验证码的校验,获取validate和seccode")
        print("gt=\n" + gt)
        print("challenge=\n" + post_data1["challenge"])
        print("\n")

        # 传参 不传参则是普通模式;传入1个参数 任何内容,则定制优化模式
        if len(args) >= 2:
            validate_seccode = input("请输入快捷复制的validate&seccode:")
            match = re.search('validate=(.*)&seccode=(.*)', validate_seccode)
            validate = match.group(1)
            seccode = match.group(2)
        else:
            validate = input("请输入验证后的validate:")
            seccode = input("请输入验证后的seccode:")

        if validate == "" or seccode == "":
            print("空值是达咩的啦~如果验证失败,可以重新运行重试~")
            return

        post_data1["validate"] = validate
        post_data1["seccode"] = seccode

        print(json.dumps(post_data1, indent=2, ensure_ascii=False))
        print("[传参内容集齐啦!开冲!]\n")
        
        captcha_key_json = await get_captcha_key(post_data1)

        try:
            if captcha_key_json["code"] != 0:
                print("失败了喵~,请求返回如下")
                print(json.dumps(captcha_key_json, indent=2, ensure_ascii=False))
                return

            print("短信验证码发送成功!")
            print(json.dumps(captcha_key_json, indent=2, ensure_ascii=False))

            print("\n请在5分钟内完成验证哦~")
            code = input("请输入手机收到的验证码:")

            if code == "":
                print("空验证码???")
                return
            else:
                post_data2["code"] = code

            post_data2["cid"] = post_data1["cid"]
            post_data2["tel"] = post_data1["tel"]
            post_data2["captcha_key"] = captcha_key_json["data"]["captcha_key"]


            login_json = await login(post_data2)

            try:
                if login_json["code"] != 0:
                    print("\n登录失败喵~请求返回如下")
                    print(json.dumps(login_json, indent=2, ensure_ascii=False))
                    return
                
                # print(json.dumps(login_json, indent=2, ensure_ascii=False))

                print("\n登录成功!!!")

                if login_json["data"]["is_new"] == True:
                    print("新注册用户")
            except Exception as e:
                print(e)
                return

        except Exception as e:
            print(e)
            return


    except Exception as e:
        print(e)
        return

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

data/country.json

{"code":0,"data":{"common":[{"id":1,"cname":"中国大陆","country_id":"86"},{"id":5,"cname":"中国香港特别行政区","country_id":"852"},{"id":2,"cname":"中国澳门特别行政区","country_id":"853"},{"id":3,"cname":"中国台湾","country_id":"886"},{"id":4,"cname":"美国","country_id":"1"},{"id":6,"cname":"比利时","country_id":"32"},{"id":7,"cname":"澳大利亚","country_id":"61"},{"id":8,"cname":"法国","country_id":"33"},{"id":9,"cname":"加拿大","country_id":"1"},{"id":10,"cname":"日本","country_id":"81"},{"id":11,"cname":"新加坡","country_id":"65"},{"id":12,"cname":"韩国","country_id":"82"},{"id":13,"cname":"马来西亚","country_id":"60"},{"id":14,"cname":"英国","country_id":"44"},{"id":15,"cname":"意大利","country_id":"39"},{"id":16,"cname":"德国","country_id":"49"},{"id":18,"cname":"俄罗斯","country_id":"7"},{"id":19,"cname":"新西兰","country_id":"64"}],"others":[{"id":22,"cname":"阿富汗","country_id":"93"},{"id":20,"cname":"阿尔巴尼亚","country_id":"355"},{"id":21,"cname":"阿尔及利亚","country_id":"213"},{"id":31,"cname":"安道尔","country_id":"376"},{"id":32,"cname":"安哥拉","country_id":"244"},{"id":33,"cname":"安提瓜岛和巴布达","country_id":"1268"},{"id":23,"cname":"阿根廷","country_id":"54"},{"id":204,"cname":"亚美尼亚","country_id":"374"},{"id":183,"cname":"阿森松岛","country_id":"247"},{"id":34,"cname":"奥地利","country_id":"43"},{"id":26,"cname":"阿塞拜疆","country_id":"994"},{"id":37,"cname":"巴哈马群岛","country_id":"1242"},{"id":40,"cname":"巴林","country_id":"973"},{"id":131,"cname":"孟加拉国","country_id":"880"},{"id":35,"cname":"巴巴多斯","country_id":"1246"},{"id":43,"cname":"白俄罗斯","country_id":"375"},{"id":52,"cname":"伯利兹","country_id":"501"},{"id":46,"cname":"贝宁","country_id":"229"},{"id":44,"cname":"百慕大群岛","country_id":"1441"},{"id":54,"cname":"不丹","country_id":"975"},{"id":51,"cname":"玻利维亚","country_id":"591"},{"id":49,"cname":"波黑","country_id":"387"},{"id":53,"cname":"博茨瓦纳","country_id":"267"},{"id":42,"cname":"巴西","country_id":"55"},{"id":193,"cname":"文莱","country_id":"673"},{"id":45,"cname":"保加利亚","country_id":"359"},{"id":55,"cname":"布基纳法索","country_id":"226"},{"id":56,"cname":"布隆迪","country_id":"257"},{"id":96,"cname":"柬埔寨","country_id":"855"},{"id":100,"cname":"喀麦隆","country_id":"237"},{"id":72,"cname":"佛得角","country_id":"238"},{"id":102,"cname":"开曼群岛","country_id":"1345"},{"id":68,"cname":"非洲中部","country_id":"236"},{"id":214,"cname":"乍得","country_id":"235"},{"id":216,"cname":"智利","country_id":"56"},{"id":77,"cname":"哥伦比亚","country_id":"57"},{"id":103,"cname":"科摩罗","country_id":"269"},{"id":75,"cname":"刚果","country_id":"242"},{"id":76,"cname":"刚果(金)","country_id":"243"},{"id":107,"cname":"库克岛","country_id":"682"},{"id":78,"cname":"哥斯达黎加","country_id":"506"},{"id":105,"cname":"克罗地亚","country_id":"385"},{"id":81,"cname":"古巴","country_id":"53"},{"id":162,"cname":"塞浦路斯","country_id":"357"},{"id":97,"cname":"捷克","country_id":"420"},{"id":58,"cname":"丹麦","country_id":"45"},{"id":59,"cname":"迪戈加西亚岛","country_id":"246"},{"id":90,"cname":"吉布提","country_id":"253"},{"id":61,"cname":"多米尼加","country_id":"1767"},{"id":62,"cname":"多米尼加代表","country_id":"1809"},{"id":63,"cname":"厄瓜多尔","country_id":"593"},{"id":27,"cname":"埃及","country_id":"20"},{"id":157,"cname":"萨尔瓦多","country_id":"503"},{"id":57,"cname":"赤道几内亚","country_id":"240"},{"id":64,"cname":"厄立特里亚","country_id":"291"},{"id":30,"cname":"爱沙尼亚","country_id":"372"},{"id":28,"cname":"埃塞俄比亚","country_id":"251"},{"id":73,"cname":"福克兰岛","country_id":"500"},{"id":65,"cname":"法罗岛","country_id":"298"},{"id":70,"cname":"斐济","country_id":"679"},{"id":71,"cname":"芬兰","country_id":"358"},{"id":67,"cname":"法属圭亚那","country_id":"594"},{"id":66,"cname":"法属波利尼西亚","country_id":"689"},{"id":95,"cname":"加蓬","country_id":"241"},{"id":74,"cname":"冈比亚","country_id":"220"},{"id":154,"cname":"格鲁吉亚","country_id":"995"},{"id":94,"cname":"加纳","country_id":"233"},{"id":215,"cname":"直布罗陀","country_id":"350"},{"id":199,"cname":"希腊","country_id":"30"},{"id":80,"cname":"格陵兰岛","country_id":"299"},{"id":79,"cname":"格林纳达","country_id":"1473"},{"id":82,"cname":"瓜德罗普岛","country_id":"590"},{"id":83,"cname":"关岛","country_id":"1671"},{"id":92,"cname":"几内亚","country_id":"224"},{"id":93,"cname":"几内亚比绍","country_id":"245"},{"id":84,"cname":"海地","country_id":"509"},{"id":87,"cname":"洪都拉斯","country_id":"504"},{"id":201,"cname":"匈牙利","country_id":"36"},{"id":47,"cname":"冰岛","country_id":"354"},{"id":209,"cname":"印度","country_id":"91"},{"id":210,"cname":"印尼","country_id":"62"},{"id":207,"cname":"伊朗","country_id":"98"},{"id":206,"cname":"伊拉克","country_id":"964"},{"id":29,"cname":"爱尔兰","country_id":"353"},{"id":208,"cname":"以色列","country_id":"972"},{"id":200,"cname":"科特迪瓦","country_id":"225"},{"id":203,"cname":"牙买加","country_id":"1876"},{"id":211,"cname":"约旦","country_id":"962"},{"id":106,"cname":"肯尼亚","country_id":"254"},{"id":89,"cname":"基里巴斯","country_id":"686"},{"id":85,"cname":"朝鲜","country_id":"850"},{"id":104,"cname":"科威特","country_id":"965"},{"id":91,"cname":"吉尔吉斯斯坦","country_id":"996"},{"id":110,"cname":"老挝","country_id":"856"},{"id":108,"cname":"拉脱维亚","country_id":"371"},{"id":111,"cname":"黎巴嫩","country_id":"961"},{"id":109,"cname":"莱索托","country_id":"266"},{"id":113,"cname":"利比里亚","country_id":"231"},{"id":114,"cname":"利比亚","country_id":"218"},{"id":112,"cname":"立陶宛","country_id":"370"},{"id":115,"cname":"卢森堡","country_id":"352"},{"id":124,"cname":"马其顿","country_id":"389"},{"id":118,"cname":"马达加斯加","country_id":"261"},{"id":121,"cname":"马拉维","country_id":"265"},{"id":119,"cname":"马尔代夫","country_id":"960"},{"id":122,"cname":"马里","country_id":"223"},{"id":120,"cname":"马耳他","country_id":"356"},{"id":123,"cname":"马里亚纳岛","country_id":"1670"},{"id":126,"cname":"马歇尔岛","country_id":"692"},{"id":125,"cname":"马提尼克岛","country_id":"596"},{"id":128,"cname":"毛里塔尼亚","country_id":"222"},{"id":127,"cname":"毛里求斯","country_id":"230"},{"id":139,"cname":"墨西哥","country_id":"52"},{"id":133,"cname":"密克罗尼西亚","country_id":"691"},{"id":135,"cname":"摩尔多瓦","country_id":"373"},{"id":137,"cname":"摩纳哥","country_id":"377"},{"id":129,"cname":"蒙古","country_id":"976"},{"id":130,"cname":"蒙特塞拉特岛","country_id":"1664"},{"id":136,"cname":"摩洛哥","country_id":"212"},{"id":138,"cname":"莫桑比克","country_id":"258"},{"id":134,"cname":"缅甸","country_id":"95"},{"id":140,"cname":"纳米比亚","country_id":"264"},{"id":143,"cname":"瑙鲁","country_id":"674"},{"id":144,"cname":"尼泊尔","country_id":"977"},{"id":86,"cname":"荷兰","country_id":"31"},{"id":145,"cname":"尼加拉瓜","country_id":"505"},{"id":146,"cname":"尼日尔","country_id":"227"},{"id":147,"cname":"尼日利亚","country_id":"234"},{"id":148,"cname":"纽埃岛","country_id":"683"},{"id":150,"cname":"诺福克岛","country_id":"672"},{"id":149,"cname":"挪威","country_id":"47"},{"id":25,"cname":"阿曼","country_id":"968"},{"id":38,"cname":"巴基斯坦","country_id":"92"},{"id":151,"cname":"帕劳","country_id":"680"},{"id":41,"cname":"巴拿马","country_id":"507"},{"id":36,"cname":"巴布亚新几内亚","country_id":"675"},{"id":39,"cname":"巴拉圭","country_id":"595"},{"id":132,"cname":"秘鲁","country_id":"51"},{"id":69,"cname":"菲律宾","country_id":"63"},{"id":50,"cname":"波兰","country_id":"48"},{"id":152,"cname":"葡萄牙","country_id":"351"},{"id":48,"cname":"波多黎各","country_id":"1787"},{"id":101,"cname":"卡塔尔","country_id":"974"},{"id":99,"cname":"聚会岛","country_id":"262"},{"id":117,"cname":"罗马尼亚","country_id":"40"},{"id":116,"cname":"卢旺达","country_id":"250"},{"id":159,"cname":"萨摩亚,东部","country_id":"684"},{"id":158,"cname":"萨摩亚,西部","country_id":"685"},{"id":168,"cname":"圣马力诺","country_id":"378"},{"id":166,"cname":"圣多美和普林西比","country_id":"239"},{"id":165,"cname":"沙特阿拉伯","country_id":"966"},{"id":161,"cname":"塞内加尔","country_id":"221"},{"id":163,"cname":"塞舌尔共和国","country_id":"248"},{"id":160,"cname":"塞拉利昂","country_id":"232"},{"id":171,"cname":"斯洛伐克","country_id":"421"},{"id":172,"cname":"斯洛文尼亚","country_id":"386"},{"id":176,"cname":"所罗门群岛","country_id":"677"},{"id":177,"cname":"索马里","country_id":"252"},{"id":141,"cname":"南非","country_id":"27"},{"id":198,"cname":"西班牙","country_id":"34"},{"id":170,"cname":"斯里兰卡","country_id":"94"},{"id":167,"cname":"圣卢西亚","country_id":"1784"},{"id":169,"cname":"圣皮埃尔和密克隆群岛","country_id":"508"},{"id":174,"cname":"苏丹","country_id":"249"},{"id":175,"cname":"苏里南","country_id":"597"},{"id":173,"cname":"斯威士兰","country_id":"268"},{"id":155,"cname":"瑞典","country_id":"46"},{"id":156,"cname":"瑞士","country_id":"41"},{"id":202,"cname":"叙利亚","country_id":"963"},{"id":179,"cname":"坦桑尼亚","country_id":"255"},{"id":178,"cname":"泰国","country_id":"66"},{"id":60,"cname":"多哥","country_id":"228"},{"id":188,"cname":"托克劳岛","country_id":"690"},{"id":180,"cname":"汤加","country_id":"676"},{"id":182,"cname":"特立尼达和多巴哥","country_id":"1868"},{"id":184,"cname":"突尼斯","country_id":"216"},{"id":186,"cname":"土耳其","country_id":"90"},{"id":187,"cname":"土库曼斯坦","country_id":"993"},{"id":181,"cname":"特克斯和凯科斯","country_id":"1649"},{"id":185,"cname":"图瓦卢","country_id":"688"},{"id":194,"cname":"乌干达","country_id":"256"},{"id":195,"cname":"乌克兰","country_id":"380"},{"id":24,"cname":"阿联酋","country_id":"971"},{"id":196,"cname":"乌拉圭","country_id":"598"},{"id":197,"cname":"乌兹别克斯坦","country_id":"998"},{"id":189,"cname":"瓦努阿图","country_id":"678"},{"id":192,"cname":"委内瑞拉","country_id":"58"},{"id":212,"cname":"越南","country_id":"84"},{"id":191,"cname":"维珍群岛(英属)","country_id":"1284"},{"id":190,"cname":"维珍群岛(美属)","country_id":"1340"},{"id":88,"cname":"维克岛","country_id":"1808"},{"id":153,"cname":"瓦利斯群岛和富图纳群岛","country_id":"1681"},{"id":205,"cname":"也门","country_id":"967"},{"id":142,"cname":"塞尔维亚","country_id":"381"},{"id":213,"cname":"赞比亚","country_id":"260"},{"id":164,"cname":"桑给巴尔岛","country_id":"259"},{"id":98,"cname":"津巴布韦","country_id":"263"}]}}

标签:并打印,python,country,json,cname,cookie,print,post,id
From: https://www.cnblogs.com/ikaros-521/p/17138065.html

相关文章

  • Python之装饰器的使用
    1.装饰器的使用场景函数执行时间的统计输出日志信息2.装饰器实现已有函数执行时间的统计importtime#装饰器函数defget_time(func):definner():begin=ti......
  • Python语言中变量名是什么?命名规则有哪些?
    在Python程序中,变量是用一个变量名表示,变量名必须是大小写英文、数字和下划线的组合,且不能用数字开头。那么Python语言中变量名是什么?变量的命名规则有哪些?具体内容请......
  • From C++ to Python and a little Java
    原创不意味着能得到“知识产权”。FromC++toPythonandalittleJava从C++到Python以及对Java的小观点OutputPython:printf'\n'C++:std::coutprintformat......
  • python excel 导出
    插入特殊符号符号表打开Excel点击“插入”-“符号”打开符号面板在“符号”面板中,点击来自下拉菜单,点击选中“Unicode(十六进制)”。点击选中特殊字符,在字符代码处即......
  • centos7编译安装python3
    1.安装依赖yum-yinstallzlib-develbzip2-developenssl-develncurses-develsqlite-develreadline-develtk-develgccmakelibffi-devel2.下载软件包wgeth......
  • 对比冒泡排序--PHP + python
    PHP写法:functionsortCustom($arr):array{for($i=0;$i<count($arr);$i++){for($j=0;$j<count($arr)-$i-1;$j++){if($arr[$......
  • python中常用的公共函数封装
    函数计算函数的执行时间1importtime23deftimer(func):4"""5用于计时的装饰器函数67:paramfunc:被装饰函数8:return:闭......
  • C、C++、python、java
    C++和Python的区别python是一种脚本语言,是解释执行的,而C++是编译语言,是需要编译后在特定平台运行的。python可以很方便的跨平台,但是效率没有C++高。Python使用缩进来区......
  • Python绘制神经网络模型图
      本文介绍基于Python语言,对神经网络模型的结构进行可视化绘图的方法。  最近需要进行神经网络结构模型的可视化绘图工作。查阅多种方法后,看到很多方法都比较麻烦,例如......
  • python 学习
    www.pythonthree.compytubePytube是一个小型的、无依赖性的Python模块,用于从Internet访问视频,必须先安装它才能使用它。当你有pip时,安装很简单。要使用pip安装Py......