• 使用 urllib.request 来访问蘑菇丁、登录


    import ssl
    import json
    #使用urllib 去访问页面
    import urllib.request as ur
    #规避警告
    context = ssl._create_unverified_context()


    # 登录函数
    def Login(info_data):

    # 把字典info_data转成字符串、再用过encoding指定的编码格式编码字符串
    test_data = json.dumps(info_data).encode()
    print("*******",test_data,"-----",type(test_data))

    request_data = ur.Request(
    #要登录的网址
    url='https://api.moguding.net:9000/session/user/v1/login',

    # login_data =是{'loginType': 'iphone', 'password': 'hope2019', 'phone': '18681989609'}
    #登录头中的数据
    data=json.dumps(info_data).encode(),

    #高速服务器自己是谁, 这里说自己是app
    headers={'Content-Type': 'application/json; charset=UTF-8'}
    )

    print(ur.Request)
    try:
    #实现登录 其中.read()是拿到内容, ,decode是以Python decode() 方法以 encoding 指定的编码格式解码字符串
    response = json.loads(ur.urlopen(request_data, context=context).read().decode())
    token = response['data']['token']

    #拿到token
    if token:
    return {
    'code': '200',
    'info': '登录成功',
    'token': token
    }

    except Exception as e:
    if str(e) == '<urlopen error Remote end closed connection without response>':
    return {
    'code': '404',
    'info': '网络链接超时'
    }
    else:
    return {
    'code': '505',
    'info': '账号或密码错误'
    }


    #获取学生日报(day)、周报(week)、月报(month)列表
    def GetReports(token, reportType):
    ids = []
    day = {"currPage": 1, "pageSize": 25, "batchId": "", "classId": "",
    "teaId": "", "reportType": reportType, "planId": "", "state": 0}
    request_data = ur.Request(

    #个人中心的网址
    url = "https://api.moguding.net:9000/usercenter/user/v1/info",
    #url='https://api.moguding.net:9000/practice/paper/v1/list',
    headers={
    'Authorization': token,
    'Content-Type': 'application/json; charset=UTF-8'
    },
    data=json.dumps(day).encode("utf-8"))
    try:
    #访问个人中心、、
    response = ur.urlopen(request_data, context=context).read().decode()
    #print("response--------------",response)
    return response
    except Exception as e:
    print(e)


    if __name__ == '__main__':
    response = Login({'loginType': 'iphone', 'password': '密码', 'phone': '手机号'})
    token = response['token']
    ids = GetReports(token,"day")
    print("个人中心的response: ", ids)
  • 相关阅读:
    online ddl与pt-osc详解
    几个重点问题回顾
    死锁及常见死锁模型
    InnoDB中锁的算法(3)
    一个幻读模型引出的记录可见性判断
    jupyter notebook的使用
    l线程池抓取lianjia
    lagou数据爬取
    爬虫代理的设置
    linux如何安装和启动mongdb
  • 原文地址:https://www.cnblogs.com/yuanjia8888/p/13470462.html
Copyright © 2020-2023  润新知