python3学习-requests使用

前面我们讲过了urllib模块,知道他是用于网络请求的,这一节讲的requests还是用于网络请求的,只不过urllib是官方模块,而requests是第三方的模块。用过的人都说他才是’人类使用的’,哈哈,我也没觉得urllib有多么尿性啊!下面我们试着回归人类的生活吧。

这是第三方的模块,所以需要手动安装,安装过程在此不表,请度娘查之。

1.首先我们导入模块:

import requests
  • 1
  • 2

2.请求url:

get请求:

 r = requests.get('https://www.baidu.com')
  • 1
  • 2

带参数的get请求:

params = {'k1':'v1','k2':'v2'}
r = requests.get('https://www.baidu.com',params=params)
print(r.url)

https://www.baidu.com?k1=1&k2=v2

post请求:

r = requests.post('https://www.baidu.com',data={'k1':'v1'})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

上面传递的参数是字典,我们也可以传递json类型的参数:

import requests
import json

params = {'k1':'v1','k2':'v2'}

url = 'http://www.baidu.com'
requests.post(url,json.dump(params))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

因为发送json格式太常见,requests模块已经为我们集成了json格式的数据,而不用我们引入json模块:

import requests

params = {'k1':'v1','k2':'v2'}

url = 'http://www.baidu.com'
requests.post(url,json=params)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

下面还有更厉害的,如果我们想post一个文件怎么办呢,哈哈requests一样可以做到的:

import requests

url = 'http://www.baidu.com'

file = {'f':('test.txt',open('test.xls','rb'),'application/nnd.ms-excel',{'Expires':'0'})} #指定文件名等信息
requests.post(url,files=file)

如果你发送一个非常大的文件作为 multipart/form-data 请求,你可能希望将请求做成数据流。默认下 requests 不支持, 但有个第三方包 requests-toolbelt 是支持的。

import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder

m = MultipartEncoder(
    fields={'field0': 'value', 'field1': 'value',
            'field2': ('filename', open('file.py', 'rb'), 'text/plain')}
    )

r = requests.post('http://httpbin.org/post', data=m,
                  headers={'Content-Type': m.content_type})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

3.获取响应信息

import requests

url = 'http://www.baidu.com'
resp = requests.post(url)
print(resp.text)
#如果这里你打印text是乱码那就是字符编码的问题啦
#你可以手动设置编码格式:
resp.encoding = 'UTF-8'

print('--------------------------------')
print(resp.content)
print('--------------------------------')
print(resp.headers)
print('--------------------------------')
print(resp.url)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

text会以带格式的文本显示请求地址的网页源码,content响应的是二进制响应体。headers显示请求头信息,url显示该次请求的url。当然还有很多方法,在此不一一细说,感兴趣大家下去过一遍。

4.对压缩格式的相应内容自动解压缩

对于gzip和deflate格式的响应数据Requests会自动解码。

我们还是以豆瓣网为例,豆瓣的返回格式是gzip的,如果使用urllib的话需要我们手动调用gzip模块进行解压缩。

import urllib.request,urllib.parse
import gzip

url = 'https://www.douban.com/'

header = {
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Encoding':'gzip, deflate, sdch, br',
    'Accept-Language':'zh-CN,zh;q=0.8',
    'Cache-Control':'max-age=0',
    'Connection':'keep-alive',
    'Host':'www.douban.com',
    'Upgrade-Insecure-Requests':'1',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'
}

resp = urllib.request.Request(url,headers=header)

print(resp)

data = urllib.request.urlopen(resp)
data = data.read()
data = gzip.decompress(data).decode('UTF-8') #从请求头Accept-Encoding中看到网页被压缩过,所以需要解压缩
print(data)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

现在有了Requests这个操作就大大缩水了:

import requests

url = 'https://www.douban.com/'

header = {
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Encoding':'gzip, deflate, sdch, br',
    'Accept-Language':'zh-CN,zh;q=0.8',
    'Cache-Control':'max-age=0',
    'Connection':'keep-alive',
    'Host':'www.douban.com',
    'Upgrade-Insecure-Requests':'1',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'
}



resp = requests.post(url,header)
print(resp.text)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

不需要手动解压,Requests已经帮我们做了判断。

5.json响应内容☆☆☆

Requests 中也有一个内置的 JSON 解码器,助你处理 JSON 数据:

import requests

r = requests.get('http://www.tianyancha.com/v2/industryGuobiao.json')
print(r.json())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

6.Cookie

如果某个响应中包含一些 cookie,你可以快速访问它们:

url = 'http://example.com/some/cookie/setting/url'
r = requests.get(url)
r.cookies['example_cookie_name']
  • 1
  • 2
  • 3
  • 4

要想发送你的cookies到服务器,可以使用 cookies 参数:

cookies = dict(cookies_are='working')
r = requests.get(url, cookies=cookies)

猜你喜欢

转载自blog.csdn.net/weixin_37405394/article/details/79543881