requests 是一个功能强大、简单易用的 HTTP 请求库,可使用 pip install requests
命令进行安装html
下面咱们将会介绍 requests 中经常使用的方法,详细内容请参考 官方文档python
在开始讲解前,先给你们提供一个用于测试的网站,http://www.httpbin.org/json
这个网站能够在页面上返回所发送 请求 的相关信息,十分适合练习使用cookie
好了,下面正式开始!网络
该方法用于向目标网址发送请求,接收响应app
该方法返回一个 Response 对象,其经常使用的属性和方法列举以下:post
response.content.decode('utf-8')
json.loads(response.text)
>>> import requests >>> response = requests.get('http://www.httpbin.org/get') >>> type(response) # <class 'requests.models.Response'> >>> print(response.url) # 返回请求网站的 URL # http://www.httpbin.org/get >>> print(response.status_code) # 返回响应的状态码 # 200 >>> print(response.encoding) # 返回响应的编码方式 # None >>> print(response.cookies) # 返回响应的 Cookie 信息 # <RequestsCookieJar[]> >>> print(response.headers) # 返回响应头 # {'Connection': 'keep-alive', 'Server': 'gunicorn/19.9.0', 'Date': 'Sat, 18 Aug 2018 02:00:23 GMT', 'Content-Type': 'application/json', 'Content-Length': '275', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true', 'Via': '1.1 vegur'} >>> type(response.content) # 返回 bytes 类型的响应体 # <class 'bytes'> >>> type(response.text) # 返回 str 类型的响应体 # <class 'str'> >>> type(response.json()) # 返回 dict 类型的响应体 # <class 'dict'>
该方法的参数说明以下:测试
url:必填,指定请求 URL网站
params:字典类型,指定请求参数,经常使用于发送 GET 请求时使用编码
>>> import requests >>> url = 'http://www.httpbin.org/get' >>> params = { 'key1':'value1', 'key2':'value2' } >>> response = requests.get(url=url,params=params) >>> print(response.text) # { # "args": { # 咱们设定的请求参数 # "key1": "value1", # "key2": "value2" # }, # "headers": { # "Accept": "*/*", # "Accept-Encoding": "gzip, deflate", # "Connection": "close", # "Host": "www.httpbin.org", # "User-Agent": "python-requests/2.19.1" # }, # "origin": "110.64.88.141", # "url": "http://www.httpbin.org/get?key1=value1&key2=value2" # }
data:字典类型,指定表单信息,经常使用于发送 POST 请求时使用
注意:此时应该使用 post 方法,只须要简单的将 get 替换成 post 便可
>>> import requests >>> url = 'http://www.httpbin.org/post' >>> data = { 'key1':'value1', 'key2':'value2' } >>> response = requests.post(url=url,data=data) >>> print(response.text) # { # "args": {}, # "data": "", # "files": {}, # "form": { # 咱们设定的表单数据 # 'key1': 'value1', # 'key2': 'value2' # }, # "headers": { # "Accept": "*/*", # "Accept-Encoding": "gzip, deflate", # "Connection": "close", # "Content-Length": "17", # "Content-Type": "application/x-www-form-urlencoded", # "Host": "www.httpbin.org", # "User-Agent": "python-requests/2.19.1" # }, # "json": null, # "origin": "116.16.107.178", # "url": "http://www.httpbin.org/post" # }
headers:字典类型,指定请求头部
>>> import requests >>> url = 'http://www.httpbin.org/headers' >>> headers = { 'USER-AGENT':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36' } >>> response = requests.get(url=url,headers=headers) >>> print(response.text) # { # "headers": { # "Accept": "*/*", # "Accept-Encoding": "gzip, deflate", # "Connection": "close", # "Host": "www.httpbin.org", # "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36" # 咱们设定的请求头部 # } # }
proxies:字典类型,指定使用的代理
>>> import requests >>> url = 'http://www.httpbin.org/ip' >>> proxies = { 'http':'182.88.178.128:8123', 'http':'61.135.217.7:80' } >>> response = requests.get(url=url,proxies=proxies) >>> print(response.text) # { # "origin": "182.88.178.128" # }
cookies:字典类型,指定 Cookie
>>> import requests >>> url = 'http://www.httpbin.org/cookies' >>> cookies = { 'name1':'value1', 'name2':'value2' } >>> response = requests.get(url=url,cookies=cookies) >>> print(response.text) # { # "cookies": { # "name1": "value1", # "name2": "value2" # } # }
auth:元组类型,指定登录时的帐号和密码
>>> import requests >>> url = 'http://www.httpbin.org/basic-auth/user/password' >>> auth = ('user','password') >>> response = requests.get(url=url,auth=auth) >>> print(response.text) # { # "authenticated": true, # "user": "user" # }
verify:布尔类型,指定请求网站时是否须要进行证书验证,默认为 True,表示须要证书验证
假如不但愿进行证书验证,则须要设置为 False
>>> import requests >>> response = requests.get(url='https://www.httpbin.org/',verify=False)
可是在这种状况下,通常会出现 Warning 提示,由于 Python 但愿咱们可以使用证书验证
若是不但愿看到 Warning 信息,可使用如下命令消除
>>> requests.packages.urllib3.disable_warnings()
timeout:指定超时时间,若超过指定时间没有得到响应,则抛出异常
exceptions 是 requests 中负责异常处理的模块,包含下面常见的异常类:
注意 :全部显式抛出的异常都继承自 requests.exceptions.RequestException
>>> import requests >>> try: response = requests.get('http://www.httpbin.org/get', timeout=0.1) except requests.exceptions.RequestException as e: if isinstance(e,requests.exceptions.Timeout): print("Time out") # Time out
【参考资料】
【爬虫系列相关文章】