最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - how to convert response to image with decoding? - Stack Overflow

programmeradmin0浏览0评论

I was using this code on python previously

with open(f"image.jpg", "wb") as fh:
    fh.write(response.content)

and it was working fine, but now I changed lib for requests and the current script doesn't work

That is the new response.text example

while old response.text looks like:

tried this code to decode unicode:

with open(f"image.jpg", "wb") as fh:
    fh.write(response.text.encode().decode('unicode_escape').encode())

but the image is not correct, it does not display.

also tried: ee = base64.encodebytes(rim.content[1:-1].decode('unicode_escape').replace('\\n','').replace('\n','').replace('\\r','').replace('\r','').encode())

and as result got:

I did consider replacing all slashes and \n but still the result string is invalid. I believe there are some decode errors or my mistakes.

I use the same headers for content type and encoding in the request, so please, if it's possible, suggest solutions not related to the request itself.

发布评论

评论列表(0)

  1. 暂无评论