• Python requests模块&session day6


    一、get请求

    格式:

    import requests
     
    req = requests.get('http://www.nnzhp.cn',data={'username':'xxx'},cookies={'k':'v'},
                       headers={'User-Agent':'Chrome'},verify=False,timeout=3)  #发送get请求,data是请求数据,
                            # cookies是要发送的cookies,headers是请求头信息,verify=False是https请求的时候要加上,要不然会报错。
                            #timeout参数是超时时间,超过几秒钟的话,就不再去请求它了,会返回timeout异常
                            #这些都可以不写,如果有的话,可以加上

    方法:

    import requests
    
    #1、如下为最基本的get请求,只有url
    r = requests.get('https://github.com/Ranxf') # 最基本的不带参数的get请求
    print(r.status_code)# 获取返回状态码
    #2、带参数的get请求
    r1 = requests.get(url='http://dict.baidu.com/s', params={'wd': 'python'})      # 带参数的get请求
    print(r1.url)#打印url;https://dict.baidu.com/s?wd=python
    #3、带参数的get请求,及其各种返回类型
    url = 'http://api.nnzhp.cn/api/user/stu_info'
    params = {'stu_name':'矿泉水'}
    r = requests.get(url,params)
    print(r.text)#返回的是字符串
    print(r.json())#返回的是json串
    print(r.content)#返回bytes类型,多用于图片、音频、视频等
    print(r.headers)#获取响应头
    print(r.cookies)#获取返回的cookie
    print(r.encoding)#获取返回的字符集
    # 如下为下载一个mp3音乐到本地的方法
    url = 'http://aliuwmp3.changba.com/userdata/userwork/1128447787.mp3'
    r = requests.get(url)#发get请求
    fw = open('music.mp3','wb')#注意此处需要用wb
    fw.write(r.content)#需要使用二进制返回并写入文件
    fw.close()

    二、post请求

    格式:

    import requests
    req2 = requests.post('http://www.nnzhp.cn',data={'username':'xxx'},cookies={'k':'v'},
                        headers={'User-Agent':'Chrome'},files={'file':open('a.txt')},timeout=3) #发送post请求,data是请求数据,
                        # cookies是要发送的cookies,headers是请求头信息,files是发送的文件,verify=False是https请求的时候要加上,
                        # 要不然会报错,timeout参数是超时时间,超过几秒钟的话,就不再去请求它了,会返回timeout异常
                        #这些都可以不写,如果有的话,可以加上

    方法:

    #post请求
    url = 'http://api.nnzhp.cn/api/user/login'
    data = {'username':'xxxx','passwd':'xxxx'}
    r = requests.post(url,data)
    print(r.text)
    
    #传cookie,header,用qq群做例子
    url = 'https://qun.qq.com/cgi-bin/qun_mgr/get_friend_list'
    d = {'bkn':qq群号}
    
    header = {'cookie':'对应的cookie'}
    r = requests.post(url,d,headers = header)
    print(r.json())

    三、关于session

    requests库的session会话对象可以跨请求保持某些参数,说白了,就是比如你使用session成功的登录了某个网站,

    则在再次使用该session对象请求该网站的其他网页都会默认使用该session之前使用的cookie等参数

    也即是说:session可以自动管理cookie

    url ='http://api.nnzhp.cn/api/user/stu_info'
    session = requests.session()
    r = session.get(url,params={'stu_name':'矿泉水'})#session  get,使用params
    r2= session.post(url,data={'stu_name':'矿泉水'})#session  post,使用data
    print(r.json())
    print(r.cookies)#查看cookie
  • 相关阅读:
    jQuery 重新温习 遗忘知识点
    正则表达式获取博客园随笔1
    用django创建一个简单的sns
    WCF小实例以及三种宿主
    iOS: imageIO完成渐进加载图片
    Excel 菜单系统
    分布式EventBus的Socket实现
    Jenkins安装plugin
    邮件系统存储设计问答
    在Windows上使用CodeLite+MinGW+Clang进行开发
  • 原文地址:https://www.cnblogs.com/candysalty/p/11111469.html
Copyright © 2020-2023  润新知