1.request库的安装
(1)requests的安装
1)通过pip安装:
pip3 install requests
2)验证安装:
在命令行或者在Python编译器中通过improt库来导入requests判断是否导入成功
2.基本用法
下面案例使用了requests库中的get方法请求来抓取页面
#导入requests库
import requests
#发送一个get请求并得到响应
response= requests.get('https://www.baidu.com')
#查看响应对象的类型
print(type(response))
#查看响应状态码
print(response.status_code)
#查看响应内容的类型
print(type(response.text))
#查看响应的内容
print(response.text)
#查看cookies
print(response.cookies)
3.GET请求
构建了一个GET请求,请求http://httpbin.org/get
import requests
r = requests.get('http://httpbin.org/get')
print(r.text)
结果:
(1)如果要添加请求参数,比如添加两个请求参数
import requests
data = {
'name':'germey',
'age':22
}
response = requests.get('http://httpbin.org/get',params=data)
print(response.text)
(2)网页的返回内容的类型是str类型的,如果它符合JSON格式,则可以使用json( )方法将其转换为字典类型,以方便解析
import requests
response = requests.get('http://httpbin.org/get')
#str类型
print(type(response.text))
#返回响应内容的字典形式
print(response.json())
#dict类型
print(type(response.json()))
但需要注意,如果返回的内容不是JSON格式,调用json( )方法便会出现错误,抛出json.decoder.JSONDecodeError异常。
4.POST请求
1)发送POST请求。
import requests
response = requests.post('http://httpbin.org/post')
print(response.text)
2)发送带有请求参数的POST请求
import requests
data = {"name":"测试"}
#post请求获取页面数据,并向页面传递数据
requests=requests.post(url='http://httpbin.org/post',data=data)
print(requests.text)
在POST请求方法中,form部分就是请求参数。
5.设置请求头
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0',
'my-test': 'Hello'
}
response = requests.get('http://httpbin.org/get', headers=headers)
print(response.text)
6.响应
import requests
response = requests.get('https://www.baidu.com/')
#响应内容(str类型)
print(type(response.text),response.text)
#响应内容(bytes类型)
print(type(response.content),response.content)
#状态码
print(type(response.status_code),response.status_code)
#响应头
print(type(response.headers),response.headers)
#Cookies
print(type(response.cookies),response.cookies)
#URL
print(type(response.url),response.url)
requests.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:('user_proxy',),
306:('switch_proxy',),
307:('temporary_redirect','temporary_moved','temporary'),
308:('permanent_redirect',),
#客户端请求错误
400:('bad_request','bad'),
401:('unauthorized',),
402:('payment_required','payment'),
403:('forbiddent',),
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:('request_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','bandwith'),
510:('not_extended',),
511:('network_authentication_required','network_auth','network_authentication')
7.处理cookies
(1)获取Cookies
import requests
response = requests.get('https://www.baidu.com')
#打印Cookies对象
print(response.cookies)
未完结。。。。。尽情期待。。。。