Python爬虫学习纪要(八):Requests 库学习笔记3

来源:互联网 发布:知豆电动汽车怎么样 编辑:程序博客网 时间:2024/06/07 02:10
2.2、基本POST请求
import requests

#设置传入post表单信息
data = {'name':'jyx', 'age':18}
#设置请求头信息
header = { "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"}
#设置请求头信息和POST请求参数(data)
r = requests.post('http://httpbin.org/post', data=data, headers=header)
print(r.text)
-----------------------------------
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "age": "18", 
    "name": "jyx"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "15", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"
  }, 
  "json": null, 
  "origin": "117.149.2.38", 
  "url": "http://httpbin.org/post"
}
-----------------------------------

3、响应
3.1、response属性
import requests

r = requests.get('http://www.jianshu.com/')
#获取响应状态码
print(type(r.status_code), r.status_code)
#获取响应头信息
print(type(r.headers), r.headers)
#获取响应头中的cookies
print(type(r.cookies), r.cookies)
#获取访问的url
print(type(r.url), r.url)
#获取访问的历史记录
print(type(r.history, r.history))
------------------------------------------------
<class 'int'> 200
<class 'requests.structures.CaseInsensitiveDict'> {'X-Content-Type-Options': 'nosniff', 'X-Runtime': '0.010363', 'Content-Type': 'text/html; charset=utf-8', 'Content-Encoding': 'gzip', 'Server': 'Tengine', 'Set-Cookie': ........
e5a4a687d27284714c0cb9c692e3115f25249c71 for www.jianshu.com/>]>
<class 'str'> http://www.jianshu.com/
<class 'list'> []
---------------------------------------------------

3.2、状态码判断
import requests

r = request.get('http://www.jianshu.com/404.html')
#使用request内置的字母判断状态码
if not r.status_code == requests.codes.ok:
    print('404-1')
r = requests.get('http://www.jianshu.com')
#使用状态码数字判断
if not r.status_code == 200:
    print('404-2')
---------------------------------------------------
404-1
---------------------------------------------------

3.3、requests内置的状态字符
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
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_statusm, multi_stati, multiple_stati
208--already_reported
226--im_used

#Redirection
300--multiple_choices
301--moved_permanently, mvoed, '\\'
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--forbidder
404--not_found
405--method_not_allowed, not_allowed
406--not_acceptable
407--proxy_authentication_required, proxy_authentication
408--request_timeout, timeout
409--confilict
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
阅读全文
0 0
原创粉丝点击