美文网首页
Request库——Python实现的简单易用的HTTP库

Request库——Python实现的简单易用的HTTP库

作者: ZzzZBbbB | 来源:发表于2019-06-15 16:19 被阅读0次

Requests —— Python实现的简单易用的HTTP库

Python的标准库中自带一个urllib模块,可以实现爬取网页的功能,但是体验不好,而Requests库继承了urllib的所有特性,并且使用起来更方便,所以目标导向的话,Requests为不二之选。

安装
pip install requests

import requests

1.发送get请求,返回一个response对象
response = requests.get('http://www.baidu.com') 

2.response对象中具有一些参数
# 查看响应内容,response.text   
# 返回的是Unicode格式的数据,经过解码后的数据,
# Requests会按照自己猜测的方式进行解码,所以如果出现乱码,可能就是这方面出问题来
print(response.text)

# 查看响应内容,response.content返回的bytes数据类型,没有经过解码
print(response.content)
print(response.content.decode('utf-8')) # 防止出现乱码的操作——自己解码

# 查看完整url地址
print(response.url)     #http://www.baidu.com/

# 查看Requests使用的编码(自己猜测的方式进行解码)
# Requests会基于HTTP头部对响应的编码作出有根据的推测。
print(response.encoding)      #ISO-8859-1  

# 查看响应码
print(response.status_code)   #200

3.可以在发送请求的同时,传递一些参数给http服务器  
kw = {'wd':'明星'}
headers = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) \
           AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36'}
response = requests.get('http://www.baidu.com/s',params=kw, headers=headers)
print(response.urk)   #'http://www.baidu.com/s?wd=%E6%98%8E%E6%98%9F'

4.发送post请求    data参数
response = requests.post("http://www.baidu.com/",data=data)

5.代理    proxies参数
# 根据协议类型,选择不同的代理
proxies = {
  'http': 'xxxxx',
  'https': 'xxxxx',
}
response = requests.get("http://www.baidu.com/",proxies=proxies)

6.cookie:如果一个响应中包含了cookie,利用cookies属性拿到这个返回的cookie值:
resp = requests.get('http://www.baidu.com/')
print(resp.cookies)  #返回CookieJar对象
                     # <RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>
print(resp.cookies.get_dict())  # 返回cookie值,字典形式   {'BDORZ': '27315'}

7.session 在跨请求时候共享某些参数,比如在同一个session实例发出的所有请求之间共享cookie值 
# 1、 创建session对象,可以保存Cookie值
session = requests.Session()
# 2、处理 headers
headers = {}
# 3、 需要登录的用户名和密码
data = {"user_name":"user_name", "password":"xxxxx"}
# 4、发送附带用户名和密码的请求,并获取登录后的Cookie值,保存在ssion里
session.post(xxx, data = data)
# 5、 session包含用户登录后的Cookie值,可以直接访问那些登录后才可以访问的页面
response = session.get(xxx)

8.不信任的SSL证书  verify 参数,默认为True,表示验证SSL证书,若是没有安全证书,会报错SSLError
verify=False (解决报错问题)

9. json  Requests库中有一个json函数
print(type(response.text))     #<class 'str'>
print(response.json())    效果等价   print(json.loads(response.text))
print(type(response.json()))   # <class 'dict'>

10.超时设置  防止一直卡在访问页面上
from requests.exceptions import ReadTimeout
try:
    response = requests.get("http://httpbin.org/get", timeout = 0.5) #设置访问时间0.5s
    print(response.status_code)
except ReadTimeout:
    print('Timeout')

相关文章

  • Request库——Python实现的简单易用的HTTP库

    Requests —— Python实现的简单易用的HTTP库 Python的标准库中自带一个urllib模块,可...

  • 2019-01-01

    python爬虫---requests库的用法 requests是python实现的简单易用的HTTP库,使用起来...

  • 爬虫相关的一些命令

    requestsrequests是python实现的最简单易用的HTTP库,建议爬虫使用requests 获取某个...

  • python爬取pm2.5数据

    requests是python实现的简单易用的HTTP库最简单常用的方法get()和post() requests...

  • Requests库基本使用

    requests是python实现的最简单易用的HTTP库,建议爬虫使用requests 获取某个网页 各种请求 ...

  • python爬虫---爬取当天杭州二手房的信息

    requests是python实现的简单易用的HTTP库,使用起来比urllib简洁很多 因为是第三方库,所以使用...

  • python requests模块

    requests是python实现的简单易用的HTTP库,使用起来比urllib简洁很多因为是第三方库,所以使用前...

  • python request库

    request库 目的:系统的分析,掌握request库 参考:http://docs.python-reques...

  • (二)requests库的学习

    requests是python实现的简单易用的HTTP库,使用起来比urllib简洁很多,比如遇到一些高级的操作,...

  • 2. urllib 请求库

    urllib 请求库 urllib是Python内置的HTTP请求库,包含request、error、parse、...

网友评论

      本文标题:Request库——Python实现的简单易用的HTTP库

      本文链接:https://www.haomeiwen.com/subject/bclbfctx.html