当前位置:   article > 正文

python - write() argument must be str, not bytes

write() argument must be str, not bytes

python -  write() argument must be str, not bytes

源代码

  1. import requests
  2. r = requests.get('https://www.baidu.com/img/bd_logo1.png')
  3. if r.status_code == 200:
  4. with open('baidu.png','w') as f:
  5. f.write(r.content)
  6. else:
  7. print('获取失败')

修改后代码

  1. import requests
  2. r = requests.get('https://www.baidu.com/img/bd_logo1.png')
  3. if r.status_code == 200:
  4. with open('baidu.png','wb') as f:
  5. f.write(r.content)
  6. else:
  7. print('获取失败')
  8. # 原因分析 open的modle设置错误,需要将'w'改为'wb' ,'b'代表允许操作字节

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/389736
推荐阅读
相关标签
  

闽ICP备14008679号