Python模块-Request

本文详细介绍了如何使用requests库进行带参数的GET和POST请求,设置超时、认证、会话管理、代理、文件上传,以及处理Cookie和响应头。通过实例演示了登录验证和后续请求处理,同时涵盖了常见异常的处理方法。
摘要由CSDN通过智能技术生成

request常用方法/属性

import requests
import json

 # 带参数的Get请求
 r = requests.get("https://www.baidu.com/s",params={'ie':"UTF-8",'wd':"国庆"})

 # 带参数的post请求
 data = {'some': 'data'}
 headers = {'content-type': 'application/json',
            'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0'}

 p = requests.post('https://api.github.com/some/endpoint', data=data, headers=headers)

# 设置超时时间(秒数)
r = requests.get('url',timeout = 1)
# 认证某些浏览器
import requests

from requests.auth import HTTPBasicAuth

response = requests.get('需要访问的网址',auth = HTTPBasicAuth('用户名','密码'))
print(response.status_code)


# 会话对象,跨请求保持某些参数
s = requests.session()
s.auth = ('auth','passwd')
s.headers = {'key':'value'}
r = s.get('url')

# 设置代理
proxies = {'http':'ip1','http':'ip2'}
requests.get('url',proxies=proxies)
# 上传文件
import requests

url = 'http://127.0.0.1:8080/upload'
files = {'file': open('/home/rxf/test.jpg', 'rb')}
files = {'file': ('report.jpg', open('/home/lyb/sjzl.mpg', 'rb'))}     #显式的设置文件名
r = requests.post(url, files=files)
print(r.text)
# Cookie 的返回对象为 RequestsCookieJar,它的行为和字典类似
# 发送cookie到服务器:
url = 'http://httpbin.org/cookies'
cookies = dict(cookies_are='working')
r = requests.get(url, cookies=cookies)
r.text()

# 返回cookie值
print(r.cookies)

# python RequestsCookieJar 与 字典 相互转换
#将CookieJar转为字典:
cookies = requests.utils.dict_from_cookiejar(r.cookies)

#将字典转为CookieJar:
#其中cookie_dict是要转换字典
cookies = requests.utils.cookiejar_from_dict(cookie_dict, cookiejar=None, overwrite=True)

# 转换完之后就可以把它赋给cookies 并传入到session中了:

s = requests.Session()
s.cookies = cookies

# 设置cookie值
import requests
# 保持登录状态
s = requests.session()
url = 'url具体值'
# 请求头
headers = {
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36",
    "Accept":"application/json, text/javascript, */*; q=0.01",
    "Accept-Encoding":"gzip, deflate"
}
r = s.post(url,headers=headers)
# 添加登录所携带的cookies
c = requests.cookies.RequestsCookieJar()
c.set("Cookie","具体cookie值")
s.cookies.update(c)
r2 = s.get(url,headers=headers)

示例:

import requests
requests.packages.urllib3.disable_warnings()   #忽略警告

def login():
    s = requests.session()  # 实例化会话对象
    loginUrl = "https://q.XXX.dos.lixinchuxing.cn/XXXX/wxlogin"
    data = {"username": "********", "password": "******"}
    header = {"Content-Type": "application/json; charset=UTF-8"}
    try:
        res = s.post(
            url=loginUrl,
            json=data,
            headers=header, verify=False)
        print(res.cookies)
        # 后续接口请求在header中要增加"X-CSRF-Token"属性,值为登录请求获取到的csrf-token值
        token = res.cookies["csrf-token"]
        s.headers.update = ({"X-CSRF-Token":token})
        #返回登录处理后的session对象
        return s
    except Exception as e:
        print("登录异常".format(e))

# 第二次请求的url
Url2 = 'https://q.test.dos.lixinchuxing.cn/api/v1/****/****/*****/evaluation/car_info'
data={"is_from_car_manage": "false"}
# 请求接口
Res2 = login().put(url=Url2,dat=data)
print(Res2.text)
# 获取请求的完整url
print(r.url)
# 获取当前的编码 ISO-8859-1
print(r.encoding)
# 设置编码
r.encoding = 'utf-8'
print(r.encoding)

# 以encoding解析返回内容,字符方式的响应体,会自动根据响应头部的字符编码进行解码
print(r.text)

# 以字节形式返回响应体,会自动解码gzip和deflate压缩
print(r.content)
# 以字典对象存储服务器响应头,不区分大小写,若键不存在则返回None
print(r.headers)
print(r.headers.get('Connection'))
# 返回发送到服务器的头信息
print(r.requests.headers)
# 返回响应状态码
print(r.status_code)
# 返回原始响应体,即 urllib 的 response 对象,使用 r.raw.read()
print(r.raw)
# 返回重定向信息 可在请求时加上 allow_redirects = false 阻止重定向
print(r.hisory)
# 查看r.ok的布尔值可以知道是否成功
print(r.ok)
# Requests中内置的JSON编码器,以JSON形式返回,前提是返回的内容确保是json格式的
print(r.json())
print(json.loads(response.text))
# 使用自定义编码对文本内容进行解码
r = requests.get('http://www.itwhy.org')
print(r.text, '\n{}\n'.format('*'*79), r.encoding)
r.encoding = 'GBK'
print(r.text, '\n{}\n'.format('*'*79), r.encoding)
# 失败请求(非200)抛出异常
print(r.raise_for_status())

request库异常

import requests
# requests.ConnectionError  网络连接异常,如DNS查询失败,拒绝连接等
# requests.HTTPError  HTTP错误异常
# requests.URLRequired  URL缺失异常
# requests.TooManyRedirects  超过最大重定向次数,产生重定向异常
# requests.ConnectTimeout  连接远程服务器超时异常
# requests.Timeout 请求URL超时,产生超时异常

import requests
from requests.exceptions import ReadTimeout, ConnectionError, RequestException

try:
  response = requests.get("http://httpbin.org/get", timeout = 0.5)
  print(response.status_code)
except ReadTimeout:
  # 超时异常
  print('Timeout')
except ConnectionError:
  # 连接异常
  print('Connection error')
except RequestException:
  # 请求异常
  print('Error')

response库异常

# 内置状态字符
# -*- coding: utf-8 -*-

from .structures import LookupDict

_codes = {

    # Informational.
    100: ('continue',),
    101: ('switching_protocols',),
    102: ('processing',),
    103: ('checkpoint',),
    122: ('uri_too_long', 'request_uri_too_long'),
    200: ('ok', 'okay', 'all_ok', 'all_okay', 'all_good', '\\o/', '✓'),
    201: ('created',),
    202: ('accepted',),
    203: ('non_authoritative_info', 'non_authoritative_information'),
    204: ('no_content',),
    205: ('reset_content', 'reset'),
    206: ('partial_content', 'partial'),
    207: ('multi_status', 'multiple_status', 'multi_stati', 'multiple_stati'),
    208: ('already_reported',),
    226: ('im_used',),

    # Redirection.
    300: ('multiple_choices',),
    301: ('moved_permanently', 'moved', '\\o-'),
    302: ('found',),
    303: ('see_other', 'other'),
    304: ('not_modified',),
    305: ('use_proxy',),
    306: ('switch_proxy',),
    307: ('temporary_redirect', 'temporary_moved', 'temporary'),
    308: ('permanent_redirect',
          'resume_incomplete', 'resume',),  # These 2 to be removed in 3.0

    # Client Error.
    400: ('bad_request', 'bad'),
    401: ('unauthorized',),
    402: ('payment_required', 'payment'),
    403: ('forbidden',),
    404: ('not_found', '-o-'),
    405: ('method_not_allowed', 'not_allowed'),
    406: ('not_acceptable',),
    407: ('proxy_authentication_required', 'proxy_auth', 'proxy_authentication'),
    408: ('request_timeout', 'timeout'),
    409: ('conflict',),
    410: ('gone',),
    411: ('length_required',),
    412: ('precondition_failed', 'precondition'),
    413: ('request_entity_too_large',),
    414: ('request_uri_too_large',),
    415: ('unsupported_media_type', 'unsupported_media', 'media_type'),
    416: ('requested_range_not_satisfiable', 'requested_range', 'range_not_satisfiable'),
    417: ('expectation_failed',),
    418: ('im_a_teapot', 'teapot', 'i_am_a_teapot'),
    421: ('misdirected_request',),
    422: ('unprocessable_entity', 'unprocessable'),
    423: ('locked',),
    424: ('failed_dependency', 'dependency'),
    425: ('unordered_collection', 'unordered'),
    426: ('upgrade_required', 'upgrade'),
    428: ('precondition_required', 'precondition'),
    429: ('too_many_requests', 'too_many'),
    431: ('header_fields_too_large', 'fields_too_large'),
    444: ('no_response', 'none'),
    449: ('retry_with', 'retry'),
    450: ('blocked_by_windows_parental_controls', 'parental_controls'),
    451: ('unavailable_for_legal_reasons', 'legal_reasons'),
    499: ('client_closed_request',),

    # Server Error.
    500: ('internal_server_error', 'server_error', '/o\\', '✗'),
    501: ('not_implemented',),
    502: ('bad_gateway',),
    503: ('service_unavailable', 'unavailable'),
    504: ('gateway_timeout',),
    505: ('http_version_not_supported', 'http_version'),
    506: ('variant_also_negotiates',),
    507: ('insufficient_storage',),
    509: ('bandwidth_limit_exceeded', 'bandwidth'),
    510: ('not_extended',),
    511: ('network_authentication_required', 'network_auth', 'network_authentication'),
}

codes = LookupDict(name='status_codes')

for code, titles in _codes.items():
    for title in titles:
        setattr(codes, title, code)
        if not title.startswith('\\'):
            setattr(codes, title.upper(), code)

# 使用
print(requests.codes.ok)
200
print(requests.codes.unordered_collection)
425
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值