• Python函数-网络操作(重要)


    1、get请求

    (1)request模块是一个第三方模块,可用于HTTP接口测试
    (2)HTTP接口常用方法:get请求、post请求、发送header、发送cookie、发送json、下载文件、上传文件
    # get请求
    url = 'http://api.nnzhp.cn/api/user/stu_info'
    req = requests.get(url, params={'stu_name': 'abc'})
    print(req.json())  # 返回的json直接帮你转成了字典
    print(req.text)  # 返回的就是字符串,如果返回的不是json的话,就要用它了
    print(req.status_code)  # 返回的http状态码
    print(req.content)  # 返回的bytes类型的,下载文件的时候用它

    2、post请求

    # post请求
    url = "http://api.nnzhp.cn/api/user/login"
    req = requests.post(url, data={"username": "niuhanyang", "passwd": "aA123456"})
    print(req.json())

    3、发送cookie

    # 发送cookie
    url="https://qun.qq.com/cgi-bin/qun_mgr/get_group_list"
    data ={"bkn": 208992859}
    
    # 第一种,传cookie的方式:将cookie值转为字典,再当做参数传入
    # s = '_qpsvr_localtk=0.9012023292190232; pgv_pvi=4462138368; pgv_si=s1034338304; uin=o1345741814; skey=@MuFHVWyJC; RK=jloomAjTbY; ptcz=96f512726408f5c2cb47965484725b46e5a4c753b1b2bbcbe8821134be7d3764; p_uin=o1345741814; pt4_token=34aj2ZvqVlX40QMhCU2XqMk6zJsv3nQtbVaR3Xeg2Yw_; p_skey=UcydxmonceN8i3FE7PGSbckH1CZ0kpjvRU8x2mqrmKo_; traceid=3664fbb14c'
    # d = {}#将字符串转换为字典
    # for c in s.split(';'):
    #     k,v = c.strip().split('=')
    #     d[k] = v
    
    # 结果如下:
    d = {'pgv_pvi': '6636933120', 'RK': 'gRZhhBpNbS', 'ptcz': '14bab564718e3e1048a09cc0e18a23f7c51f20d5b93da610cc1427f51f63a2f8', 'pgv_pvid': '4990195883', 'ts_uid': '5190463916', 'uin': 'o0511402865', 'pgv_si': 's7505852416', 'skey': '@2ttDS8Ljw', 'p_uin': 'o0511402865', 'pt4_token': 'AgqIsYBhO1b82zx1N4SxoGpCxGV0d38ss7jCI1nYfIg_', 'p_skey': '9nlMjw4Uy44*Hu5iL3DOFonmAtZtExiniLykrsIRKmM_', 'traceid': '14035c8a79'}
    req = requests.post(url,data,cookies=d)
    print(req.json())

    4、发送header

    # 发送header 
    # #第二种传cookie的方式:直接把cookie当做字符串,传入
    header = {'cookie':"pgv_pvi=6636933120; RK=gRZhhBpNbS; ptcz=14bab564718e3e1048a09cc0e18a23f7c51f20d5b93da610cc1427f51f63a2f8; pgv_pvid=4990195883; ts_uid=5190463916; uin=o0511402865; pgv_si=s7505852416; skey=@2ttDS8Ljw; p_uin=o0511402865; pt4_token=AgqIsYBhO1b82zx1N4SxoGpCxGV0d38ss7jCI1nYfIg_; p_skey=9nlMjw4Uy44*Hu5iL3DOFonmAtZtExiniLykrsIRKmM_; traceid=14035c8a79"}
    req = requests.post(url,data,headers=header) #headers中传的header的字典key必须为'cookie'
    print(req.json())

    5、发送json

    #传json(钉钉)
    url = "https://oapi.dingtalk.com/robot/send?access_token=44402c9408df8cf3f429c02a20399fc34604f98cf572fcaeaa3f9592426176a7"
    today = datetime.datetime.now()
    d = {"msgtype": "text", "text": {"content": "现在是%s,大家不要忘记我哦!暗号:besttest" % today}}
    req = requests.post(url, json=d) #指定上传参数类型为json
    print(req.json())
    print(req.cookies)  # 获取cookies

    6、下载文件 

    #下载文件
    url = "https://q4.qlogo.cn/g?b=qq&nk=516481730&s=140" #获取QQ用户头像接口
    req = requests.get(url)
    
    f = open('wjl.jpg', 'wb') #打开文件,wb为写字节格式
    f.write(req.content)  #content获取所有的字节数据
    f.close()

    7、上传文件

    #上传文件
    url = "http://api.nnzhp.cn/api/file/file_upload"
    f = open('wjl.jpg', 'rb')
    # 1、文件上传content-type为json格式时
    # data = {'file':f}
    # 2、文件上传格式为form-data时
    # data = {'file': (filename, f, ''image / jpeg'')}
    data = {'file': f}  # 根据接口文档参数来
    r = requests.post(url, files=data)  #指定上传参数类型为files
    print(r.json())
    f.close()
    
    
  • 相关阅读:
    bestcoder#45 1002 求区间的逆序数 树状数组
    poj2299 Ultra-QuickSort 树状数组求逆序数
    codeforces#323(div2) C. GCD Table 贪心
    bestcoder#58(div2) 1002 LCS 置换
    codeforces#321(div2) E. Kefa and Watch
    1sting
    HDU1013 Digital Roots
    【编程思想】【设计模式】【行为模式Behavioral】catalog
    【编程思想】【设计模式】【行为模式Behavioral】chain
    【编程思想】【设计模式】【结构模式Structural】代理模式Proxy
  • 原文地址:https://www.cnblogs.com/ccxm/p/13537359.html
Copyright © 2020-2023  润新知