zoukankan      html  css  js  c++  java
  • 请求和响应

    http协议里需要关注的

    请求需要关注的东西 requests

    url : 告诉浏览器,你要去哪里

    Method:

    ​ get:传递数据:?&拼在url后面

    ​ 数据:url?key=value&key=value

    ​ post:

    ​ 请求体:

    ​ form data

    ​ 文件类型files

    ​ json

    headers:

    ​ cookie:保存用户登录状态

    ​ User-Agent:告诉服务器你是谁

    ​ refere:告诉服务器你从哪里来

    ​ 服务器规定的特殊字段

    2.2 请求需要关注的东西 response

    Status Code:

    ​ 2xx

    ​ 请求成功(不一定)---后台程序员自己规定的---不能用作请求成功的唯一判断标准

    ​ 3xx

    ​ 重定向

    响应头:

    ​ location:重定向地址

    ​ set_cookie:设置cookie

    ​ 服务器规定的特殊字段

    响应体:

    ​ 1.html代码(css,html,js)

    ​ 2.json

    ​ 3.二进制(图片,视频,音频)

    3、 常用请求库、解析库、数据库的用法

    3.1 常用请求库 测试网站:http://httpbin.org/get

    request库

    安装:pip install requests

    使用

    请求

    ①get请求:

    				响应对象 = requests.get(......)
    
    ​				**参数:**
    
    ​					url:
    
    ​					headers = {}
    
    ​					cookies = {}        优先级低于headers里的cookie字段
    
    ​					params = {}	
    
    ​					proxies = {'http':‘http://ip:端口’}
    
    ​					timeout = 0.5
    
    ​					allow_redirects = True
    

    ②post请求:

    				响应对象 = requests.post(......)
    
    ​				**参数:**
    
    ​					url:
    
    ​					headers = {}
    
    ​					cookies = {}
    
    ​					data = {}
    
    ​					json = {}
    
    ​					files = {‘file’:open(...,‘rb’)}
    
    ​					timeout = 0.5
    
    ​					allow_redirects = False
    

    自动保存cookie的请求:

    			session = requests.session()
    
    ​			r = session.get(......)
    
    ​			r = session.post(......)
    	  补充:(保存cookie到本地)
    	  	import http.cookiejar as cookielib
    	  	session.cookie = cookielib.LWPCookieJar()
    	  	session.cookie.save(filename='1.txt')
    	  	
    	  	session.cookies.load(filename='1.txt')
    

    响应:

    			r.url
    
    ​			r.text    常用
    
    ​			r.encoding = 'gbk'    常用
    
    ​			r.content     常用
    
    ​			r.json()      常用
     
    ​			r.status_code    用的少
    
    ​			r.headers  
    
    ​			r.cookies  
    
    ​			r.history   
    
  • 相关阅读:
    Linq聚合操作之Aggregate,Count,Sum,Distinct源码分析
    Linq分区操作之Skip,SkipWhile,Take,TakeWhile源码分析
    Linq生成操作之DefautIfEmpty,Empty,Range,Repeat源码分析
    Linq基础操作之Select,Where,OrderBy,ThenBy源码分析
    PAT 1152 Google Recruitment
    PAT 1092 To Buy or Not to Buy
    PAT 1081 Rational Sum
    PAT 1084 Broken Keyboard
    PAT 1077 Kuchiguse
    PAT 1073 Scientific Notation
  • 原文地址:https://www.cnblogs.com/SkyOceanchen/p/12168198.html
Copyright © 2011-2022 走看看