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


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

    • httpbin

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

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

      二、使用python抓去百度主页

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

      三、get传参

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

      输出:

       1 http://www.httpbin.org/post
       2 {
       3   "args": {}, 
       4   "data": "", 
       5   "files": {}, 
       6   "form": {
       7     "cnblog": "hyyq", 
       8     "qq": "34782655"
       9   }, 
      10   "headers": {
      11     "Accept": "*/*", 
      12     "Accept-Encoding": "gzip, deflate", 
      13     "Connection": "close", 
      14     "Content-Length": "23", 
      15     "Content-Type": "application/x-www-form-urlencoded", 
      16     "Host": "www.httpbin.org", 
      17     "User-Agent": "python-requests/2.18.4"
      18   }, 
      19   "json": null, 
      20   "origin": "183.230.42.10", 
      21   "url": "http://www.httpbin.org/post"
      22 }
      23 
      24 [Finished in 1.3s]

      二、发送json数据
      例子:

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

      输出:

       1 http://www.httpbin.org/post
       2 {
       3   "args": {}, 
       4   "data": "{"qq": "34782655", "cnblog": "hyyq"}", 
       5   "files": {}, 
       6   "form": {}, 
       7   "headers": {
       8     "Accept": "*/*", 
       9     "Accept-Encoding": "gzip, deflate", 
      10     "Connection": "close", 
      11     "Content-Length": "36", 
      12     "Content-Type": "application/json", 
      13     "Host": "www.httpbin.org", 
      14     "User-Agent": "python-requests/2.18.4"
      15   }, 
      16   "json": {
      17     "cnblog": "hyyq", 
      18     "qq": "34782655"
      19   }, 
      20   "origin": "183.230.42.10", 
      21   "url": "http://www.httpbin.org/post"
      22 }
      23 
      24 [Finished in 1.0s]
    • 上传文件
      例子:
      1 #上传文件
      2 import requests
      3 # print help(requests)
      4 post_url = 'http://www.httpbin.org/post'
      5 files = {'file':open('yyc.txt','rb')} #这里rb是以读的方式用二进制格式打开
      6 r = requests.post(post_url,files=files)
      7 print r.content

      输出:

       1 {
       2   "args": {}, 
       3   "data": "", 
       4   "files": {
       5     "file": "data:application/octet-stream;base64,xOO6ww=="
       6   }, 
       7   "form": {}, 
       8   "headers": {
       9     "Accept": "*/*", 
      10     "Accept-Encoding": "gzip, deflate", 
      11     "Connection": "close", 
      12     "Content-Length": "147", 
      13     "Content-Type": "multipart/form-data; boundary=8bffc774646942deaa8a314f8675d879", 
      14     "Host": "www.httpbin.org", 
      15     "User-Agent": "python-requests/2.18.4"
      16   }, 
      17   "json": null, 
      18   "origin": "183.230.42.10", 
      19   "url": "http://www.httpbin.org/post"
      20 }
      21 
      22 [Finished in 1.0s]
  • 相关阅读:
    人工智能与信号处理--看知乎问答有感.
    nginx 负载均衡及反向代理
    sqlserver 分页查询
    sqlserver 游标使用
    ffmpeg参数编码大全
    C# 阿里云查询、删除文件
    视频分片上传+C#后端合并
    ffmpeg 视频合并
    C# 根据链接提取div内容
    electron-vue 设置cookie
  • 原文地址:https://www.cnblogs.com/hyyq/p/8976126.html
Copyright © 2020-2023  润新知