python中request获取数据_Python requests包返回数据的获取方式

requests包访问网络后常用的获取数据方式有3种,今天我们就分别对3种方式进行介绍一下

1. response.text

response.text返回的是Unicode格式,通常需要转换为utf-8格式,否则就是乱码

2. response.json()

response.json()等同于json.loads(response.text)方法

3.response.content

response.content是二进制模式,可以下载图片视频之类的,如果想看的话需要decode成utf-8格式。

示例代码:

import requests

import json

response = requests.get("http://httpbin.org/get")

a = response.text

b = response.json()

print(response.text)

print(type(response.text))

print(response.json())

print(json.loads(response.text))

print(type(response.json()))

从下图中可以看出,经过json()函数的处理,虽然是相同的字符,已经转为字典的结构。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值