Python爬虫从入门到进阶(3)之requests的使用

快速上手(官网地址:http://www.python-requests.org/en/master/user/quickstart/)

发送请求

首先导入Requests模块

import requests

试着获取一个网页

r = requests.get('https://api.github.com/events')

返回的 r 是 Response 对象,可以从这个对象中获得所有信息。

Requests 简单的 API 意味着所有 HTTP 请求类型都是显而易见的。例如,可以这样发送一个 HTTP POST 请求:

 r = requests.post('https://httpbin.org/post', data={'key': 'value'})

传递 URL 参数

关于 URL 的查询字符串(query string)传递某种数据,如果手动构建 URL,数据以key/value的形式出现在 URL的?后面,例如:httpbin.org/get?key=value

Requests允许参数使用这个params 关键字参数,以字符串字典的形式提供参数。如果你想传递 key1=value1 和 key2=value2 到 httpbin.org/get ,那么你可以使用如下代码:

def url_params():
    payload = {'key1': 'value1', 'key2': 'value2'}
    r = requests.get("http://httpbin.org/get", params=payload)
    print(r.url)
    # http://httpbin.org/get?key1=value1&key2=value2

也可以将列表传入

# 2.传递 URL 参数,可以将一个列表作为值传入
def url_params_2():
    payload = {'key1': 'value1', 'key2': ['value2', 'value3']}
    r = requests.get("http://httpbin.org/get", params=payload)
    print(r.url)
    # http://httpbin.org/get?key1=value1&key2=value2&key2=value3

响应内容

Requests 会自动解码来自服务器的内容。大多数 unicode 字符集都能被无缝地解码。

# 3.响应内容
def response_text():
    r = requests.get('https://httpbin.org/get')
    print(r.text)

二进制响应内容

Requests 会自动为你解码 gzip 和 deflate 传输编码的响应数据。

# 4.二进制响应内容
def response_content():
    r = requests.get('https://httpbin.org/get')
    print(r.content)

json 响应内容

# 5.json响应内容
def response_json():
    r = requests.get('https://httpbin.org/get')
    print(r.json())

如果 JSON 解码失败, r.json() 就会抛出一个异常。例如,响应内容是 204 (No Content),或者响应包含无效的 json,尝试访问 r.json() 将会抛出 ValueError: No JSON object could be decoded 异常。

值得注意的是调用r.json()成功并不意味着请求响应成功。有些服务器响应失败会返回 JSON 对象的失败信息。可以通过判断r.raise_for_status() 或者 r.status_code  验证请求是不是成功

定制请求头

如果你想为请求添加 HTTP 头部,只要简单地传递一个 dict 给 headers 参数就可以了。

# 6.定制请求头
def add_header():
    url = 'https://httpbin.org/get'
    headers = {
        'User-Agent':'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 2.0.50727; SLCC2; .NET '
    }

    r = requests.get(url=url,headers=headers)
    print(r.text)

更加复杂的 post 请求

通常,要想发送表单形式的数据,只需简单地传递一个字典给 data 参数,在发出请求时会自动编码为表单形式:

# 7.复杂的 post 请求
def complicated_post():
    payload = {'key1': 'value1', 'key2': 'value2'}
    r = requests.post("https://httpbin.org/post", data=payload)
    print(r.text)

data 参数每个键可以有多个值,可以将元组列表或列表作为值的字典来实现,通常用于将表单中多个元素使用相同的键时使用

def complicated_post_2():
    payload_tuples = [('key1', 'value1'), ('key1', 'value2')]
    r1 = requests.post('https://httpbin.org/post', data=payload_tuples)
    print(r1.text)
    payload_dict = {'key1': ['value1', 'value2']}
    r2 = requests.post('https://httpbin.org/post', data=payload_dict)
    print(r1.text == r2.text) # False,官网是 True ???

用时候不需要用表单编码传递数据,可以使用字符串传递数据,例如

def complicated_post_3():
    url = 'https://api.github.com/some/endpoint'
    payload = {'some': 'data'}
    r = requests.post(url, data=json.dumps(payload))
    print(r.text)

也可以直接传递 json 参数,例如:

def complicated_post_4():
    url = 'https://api.github.com/some/endpoint'
    payload = {'some': 'data'}
    r = requests.post(url, json=payload)
    print(r.text)

注意:如果已经传递了 data 或者 files 参数,json 参数会被忽略掉

在请求中使用 json 参数会改变header 头Content-Type的值为application/json

POST一个多部分编码(Multipart-Encoded)的文件

Requests使上传Multipart-Encoded文件变得很简单

#  8.post 上传 multipart-Encoded 文件
def post_mulutipart_file():
    url = 'https://httpbin.org/post'
    files = {'file': open('report.xls', 'rb')}
    r = requests.post(url, files=files, timeout=10)
    print(r.text)

可以直接设置文件名称,content_type,和 header

def post_mulutipart_file_2():
    url = 'https://httpbin.org/post'
    files = {'file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}
    r = requests.post(url, files=files, timeout=10)
    print(r.text)

如果想传递字符串让服务器以文件的方式接收,可以如下设置:

def post_mulutipart_file_3():
    url = 'https://httpbin.org/post'
    files = {'file': ('report.csv', 'some,data,to,send
another,row,to,send
')}
    r = requests.post(url, files=files, timeout=10)
    print(r.text)

注意:requests 不支持传递超大文件

响应状态码

# 9.响应状态码
def response_status_code():
    r = requests.get('https://httpbin.org/get', timeout=10)
    print(r.status_code)
    print(r.status_code == requests.codes.ok)

    # print(raise_for_status)


# 请求错误状态码
def error_response_status_code():
    bad_r = requests.get('https://httpbin.org/status/404')
    print(bad_r.status_code)
    print(bad_r.raise_for_status())

响应头

# 10.响应头
def response_header():
    r = requests.get('https://httpbin.org/get', timeout=10)
    print(r.headers)
    print(r.headers['Content-Type'])
    print(r.headers.get('content-type'))

Cookies

# 11.cookie
def cookies():
    # 读取响应内容的 cookie
    # url = 'http://example.com/some/cookie/setting/url'
    # r = requests.get(url)
    # print(r.cookies['example_cookie_name'])

    # 发送 cookie 到服务器
    url = 'https://httpbin.org/cookies'
    cookies = dict(cookies_are='working')
    r = requests.get(url, cookies=cookies, timeout=10)
    print(r.text)

 Cookie 在RequestsCookieJar中返回,起作用相当于一个字典,但是也提供更完整的接口,适合在多个域或路径上使用,Cookie jars也能在请求时传递

def cookiejar():
    jar = requests.cookies.RequestsCookieJar()
    jar.set('tasty_cookie', 'yum', domain='httpbin.org', path='/cookies')
    jar.set('gross_cookie', 'blech', domain='httpbin.org', path='/elsewhere')
    url = 'https://httpbin.org/cookies'
    r = requests.get(url, cookies=jar)
    print(r.text)

重定向和历史

默认情况下,请求将对除HEAD之外的所有谓词执行位置重定向。

可以使用响应对象的history属性来跟踪重定向。

Response.history列表包含为完成请求而创建的 Response 对象,该列表从最早的响应到最近的响应

# 12.重定向和历史记录
def redirection_history():
    r = requests.get('http://github.com/')
    print(r.url)
    print(r.status_code)
    print(r.history)

如果使用 GET, OPTIONS, POST, PUT, PATCH 或者 DELETE,可以使用allow_reredirect参数禁用重定向处理

使用 HEAD 启用重定向

def redirection_history_2():
    # 禁用重定向
    r = requests.get('http://github.com/', allow_redirects=False)
    print(r.status_code)
    print(r.history)

    # 使用 HEAD 启用重定向
    r = requests.head('http://github.com/', allow_redirects=True)
    print(r.status_code)
    print(r.history)

超时

可以使用 timeout 参数告诉请求等待几秒停止响应,几乎所有的生产代码的所有请求中都使用这个参数,不然可能导致程序无限期挂起

注意:超时不是整个响应下载的时间限制;相反,如果服务器没有在超时时间限制内发出响应,则会引发异常。如果没设置超时则请求不会超时

错误和异常

如果出现网络问题(例如DNS失败、拒绝连接等),请求将引发ConnectionError异常。

如果HTTP请求返回不成功的状态代码,则Response.raise_for_status()将引发HTTPError。

如果请求超时,将引发超时异常。

 代码下载

原文地址:https://www.cnblogs.com/zimengfang/p/10141004.html