十requests
Posted mr-chenshuai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了十requests相关的知识,希望对你有一定的参考价值。
urllib在处理网页验证和Cookies时,需要写Opener和Handler来处理,为了更方便实现这些操作,就有了requests
基本实例
- urllib库的urlopen()方法实际上是以GET方式请求网页,对应requests中的响应方法就是get()
- 调用get()方法实现和urlopen()相同的操作,得到一个Response对象,然后分别输出对象类型、状态码、内容、内容类型、Cookies
- 其他请求都可以用一句话实现:
1、GET请求
- 1、利用requests构建GET请求
- 最简单的GET请求,请求http://httpbin.org/get,如果是GET请求,网站返回响应的请求信息
- 构建带参数的GET请求
- 直接写,不够人性化
- 最好用字典存储
- URL链接自动构成了带参数形式
- 网页返回类型实际是str类型的,但是这里是JSON格式的,如果想要直接解析结果,得到一个字典,可以直接调用json()方法
- json()将返回结果是JSON格式的字符串转化成字典格式
- 如果返回的不是JSON格式,就会出现解析错误:json.decoder.JSNDwcodeError异常
- 2、抓取网页、添加请求头
- 新闻开头的a标签
- 3、抓取二进制数据(音频、图片、视频等)
- 抓取GitHub的站点图标
- 结果前两行是r,.text,乱码了,这是因为图标是二进制数据,直接转换成str,后一行是b开头的bytes类型数据
- 将图片保存
2、POST请求
- requests实现POST请求
3、响应
requests提供了一个内置的状态码查询对象resquests.codes
返回码和响应的查询条件
# 信息性状态码 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‘,), # 重定向状态码 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 # 客户端错误状态码 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‘,), # 服务端错误状态码 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‘)
判断结果是不是404状态,可以用requests.codes.not_found
来比对
以上是关于十requests的主要内容,如果未能解决你的问题,请参考以下文章
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)(代码片段
C#-WebForm-★内置对象简介★Request-获取请求对象Response相应请求对象Session全局变量(私有)Cookie全局变量(私有)Application全局公共变量Vi(代码片段