Requests: 让 HTTP 服务人类(Post请求)

requests的post登录

  #基于POST请求
  import requests
  import json
  data = {'name':'boy','age':'21'}
  response = requests.post("http://httpbin.org/post",data=data)
  print(response.text)

  #加headers
  data = {'name':'boy','age':'21'}
  headers = {
        "User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36"
  }
  response2 =                     requests.post("http://httpbin.org/post",data=data,headers=headers)
  print(response2.json())

  #响应response属性
  import requests
  response3 = requests.get("http://www.jianshu.com")
  print(type(response3.status_code),response3.status_code)
  print(type(response3.headers),response3.headers)
  print(type(response3.cookies),response3.cookies)
  print(type(response3.url),response3.url)
  print(type(response3.history),response3.history)

  #状态吗判断:
  response4 = requests.get("http://www.jianshu.com")
  exit() if not response4.status_code == 200 else print("Request         Success")

  #高级操作
  #1、文件上传
  files = {'file':open('favicon.ico','rb')}
  response5 = requests.post("http://httpbin.org/post",files=files)
  print(response5.text)

  #获取cookie
  response6 = requests.get("http://www.baidu.com")
  print(response6.cookies)
  for key,value in response6.cookies.items():
      print(key + '=' +value)


  #会话维持吗、用来模拟登录
  """
  同一浏览器中要实现set get,这时需要用到requests里的Session对象
  用session 对象发起两次get请求,先访问se 在访问get 这时就相当于在一个浏览器中发起两次请求
  模拟了一次对话,做模拟登录时适用
  """
  s = requests.Session()
  s.get("http://httpbin.org/cookies/set/number/123445")
  response7 = s.get("http://httpbin.org/cookies")
  print(response7.text)

  #证书验证
  """
  verify = False用来验证是否需要验证证书,不添加的话会报错,添加上不会报错但是有一个警告
  消除警告信息,导入包from requests.packages import urllib3使用urllib3.disable_warnings()方法
  """
  from requests.packages import urllib3
  urllib3.disable_warnings()
  response8 = requests.get("https://www.12306.cn",verify = False)
  print(response8.status_code)
  手动消除,
  requests.get(,cert=("https://www.12306.cn",'/path/client.cert','/path/client.key'))
  s = requests.Session()
  s.cert = '/path/client.cert'
 #设置代理,如果ip代理有密码则,需要传入用户名和密码,http://user:[email protected]:8080/
  proxies = {
       "http":"http://61.91.52.86:8080",
       "http":"http://183.30.197.17:9797"
  }
  response9 = requests.get("http://httpbin.org",proxies=proxies)
  print(response9.text)
  #如果使用的是socks代理则需要修改后边的5是socks提供
  """
  proxies = {
      "http":"socks5://61.91.52.86:8080",
      "http":"socks5://183.30.197.17:9797"
    }
   """
  #超时设置,限制服务器应答速度
  from requests.exceptions import ReadTimeout
  try :
      response10 = requests.get("http://httpbin.org",timeout=2)
      print(response10.status_code)
  except ReadTimeout:
      print("timeout")


  #认证设置 需要输入用户名和密码
  from requests.auth import HTTPBasicAuth
  response10 = requests.get("http://httpbin.org",auth=HTTPBasicAuth('user','passwd'))

  #异常处理
  from requests.exceptions import
  ReadTimeout,HTTPError,RequestException
  try:
      response11 = requests.get("http://httpbin.org/get",timeout=0.5)
  except ReadTimeout:
      print('Timeout')
  except HTTPError:
      print('http error')
  except RequestException:
      print('Error')

你可能感兴趣的:(Requests: 让 HTTP 服务人类(Post请求))