【个人笔记向】Python的requests中的content和text的区别

此帖子主要起提醒自己的作用,如想做更深了解,推荐阅读:https://blog.csdn.net/GeekLeee/article/details/52816268

import requests
import json
url = 'https://fanyi.baidu.com/sug'
trfconect=input("请输入翻译内容:")
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0',
"TE": "Trailers",

"Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
           # "content-length": str(len(trfconect)+3),(走了一些弯路,有的帖子说百度翻译和这个有关,我看到请求头确实有。
           #*值得一提的是谷歌浏览器在https://fanyi.baidu.com/sug这个请求中F12开发工具network中只有英翻中的显示,没有中翻英的,而火狐浏览器则都有。)
           }  
data={
'kw':trfconect,
}
response = requests.post(url,data=data,headers=headers)
print(type(response.text))
print(type(response.content))
str1=response.content.decode('utf-8')
p=json.loads(str1)
print(p)
print(p['data'][0]['v'])

输出结果

请输入翻译内容:python
<class 'str'>
<class 'bytes'>
{'errno': 0, 'data': [{'k': 'python', 'v': 'n. 蟒; 蚺蛇;'}, {'k': 'pythons', 'v': 'n. 蟒; 蚺蛇;  python的复数;'}]}
n. 蟒; 蚺蛇;

可以看到response.text的类型是str,
response.content的类型是bytes。
就爬取图片而言,用的就是content,不然就会报错TypeError: a bytes-like object is required, not 'str'
自学的python,写的很乱,还望包涵,如想做更深了解,推荐阅读:https://blog.csdn.net/GeekLeee/article/details/52816268

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值