Python基础笔记系列十二:requests模块的简单应用

时间:2022-10-27 15:21:39

  本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!!

  • httpbin

    httpbin这个网站能测试 HTTP 请求和响应的各种信息,
    比如 cookie、ip、headers 和登录验证等,且支持 GET、POST 等多种方法,
    对 web 开发和测试很有帮助。它用 Python + Flask 编写,是一个开源项目。
    官方网站:http://httpbin.org/
    开源地址:https://github.com/Runscope/httpbin

  • get请求
    一、简单get请求数据
     import requests
    get_url = 'http://www.httpbin.org/get'
    r = requests.get(get_url) #发送get请求
    print r
    print help(r)
    print r.text #接收的内容in bytes
    print r.content #接收的内容in unicode

    二、使用python抓去百度主页

     import requests
    r = requests.get('http://www.baidu.com')
    print r.content
    #打开一个文件,并存入网页信息
    with open('baidu.html','w') as fil:
    fil.write(r.content)

    三、get传参

     #可以先通过help函数来查看下get方法的文档说明
    import requests
    # print help(requests.get)
    get_url = 'http://www.httpbin.org/get'
    myparams={'qq':''}
    # r = requests.get(url=get_url,params=myparams)
    r = requests.get(get_url,myparams)
    print r.url #请求的url,拼接url和参数
    print r.content #返回的数据
  • post请求
    一、发送请求体
    例子:
     import requests
    #先来查看下post方法的文档使用说明
    # print help(requests.post)
    #通过data来传参,相当于传的表单,在返回的数据中可以看出
    post_url = 'http://www.httpbin.org/post'
    myData={'qq':'','cnblog':'hyyq'}
    r = requests.post(url = post_url,data = myData)
    print r.url #请求的url,请求体就是参数
    print r.content #返回的数据

    输出:

     http://www.httpbin.org/post
    {
    "args": {},
    "data": "",
    "files": {},
    "form": {
    "cnblog": "hyyq",
    "qq": "34782655"
    },
    "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Connection": "close",
    "Content-Length": "23",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "www.httpbin.org",
    "User-Agent": "python-requests/2.18.4"
    },
    "json": null,
    "origin": "183.230.42.10",
    "url": "http://www.httpbin.org/post"
    } [Finished in 1.3s]

    二、发送json数据
    例子:

     import requests,json
    post_url = 'http://www.httpbin.org/post'
    myData={'qq':'','cnblog':'hyyq'}
    r = requests.post(url = post_url,json = myData)
    # r = requests.post(url = post_url,data = json.dumps(myData))
    print r.url #请求的url,发送json数据
    print r.content #返回的数据

    输出:

     http://www.httpbin.org/post
    {
    "args": {},
    "data": "{\"qq\": \"34782655\", \"cnblog\": \"hyyq\"}",
    "files": {},
    "form": {},
    "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Connection": "close",
    "Content-Length": "36",
    "Content-Type": "application/json",
    "Host": "www.httpbin.org",
    "User-Agent": "python-requests/2.18.4"
    },
    "json": {
    "cnblog": "hyyq",
    "qq": "34782655"
    },
    "origin": "183.230.42.10",
    "url": "http://www.httpbin.org/post"
    } [Finished in 1.0s]
  • 上传文件
    例子:
     #上传文件
    import requests
    # print help(requests)
    post_url = 'http://www.httpbin.org/post'
    files = {'file':open('yyc.txt','rb')} #这里rb是以读的方式用二进制格式打开
    r = requests.post(post_url,files=files)
    print r.content

    输出:

     {
    "args": {},
    "data": "",
    "files": {
    "file": "data:application/octet-stream;base64,xOO6ww=="
    },
    "form": {},
    "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Connection": "close",
    "Content-Length": "147",
    "Content-Type": "multipart/form-data; boundary=8bffc774646942deaa8a314f8675d879",
    "Host": "www.httpbin.org",
    "User-Agent": "python-requests/2.18.4"
    },
    "json": null,
    "origin": "183.230.42.10",
    "url": "http://www.httpbin.org/post"
    } [Finished in 1.0s]