gpt4 book ai didi

arrays - 为什么用reqwest下载的PNG图像的字节与使用Python下载的字节不同?

转载 作者:行者123 更新时间:2023-12-03 11:41:42 24 4
gpt4 key购买 nike

我正在尝试使用reqwest库下载PNG文件,但是当我下载它时,我看到一种奇怪的行为,尊重其他编程语言,例如:Python。
例如:

let content = reqwest::get("https://www.google.es/images/searchbox/desktop_searchbox_sprites302_hr.png").await?;
如果我将结果打印为字节数组( println!("{:?}", content.text().await?.as_bytes());
[ 191, 189, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 40, 0, 0, 0, 82, 8, 3, 0, 0, 0, 17,  191, 189, 102,  191, 189, 0, 0, 0, 108, 80, 76, 84, 69, 0, 0, 0,  191, 189,  191, 189,  191, 189,...]
但是,使用Python请求的结果是:
[137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 40, 0, 0, 0, 82, 8, 3, 0, 0, 0, 17, 153, 102, 248, ...]
在Rust版本中,我看到了很多 191, 189。这个序列在整个数组中重复很多,但是在Python中根本没有出现。
我在Rust中做错什么了吗?

最佳答案

I see a lot of 191, 189


最好将其视为 EF, BF, BD,即将 Unicode replacement character编码为 UTF-8。二进制数据不是文本数据。您不应将 text 用于二进制数据,而应使用 bytes
const URL: &str = "https://www.google.es/images/searchbox/desktop_searchbox_sprites302_hr.png";

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let content = reqwest::get(URL).await?;
let bytes = content.bytes().await?;
println!("{:x?}", &bytes[..]);

Ok(())
}
[89, 50, 4e, 47, d, a, 1a, a, 0, 0, 0, d, 49, 48, 44, 52, 0, 0, 0, 28, 0, 0, 0, 52, 8, 3, 0, 0, 0, 11, 99, 66, f8, 0, 0, 0, 6c, 50, 4c, 54, 45, 0, 0, 0, 9f, ...

关于arrays - 为什么用reqwest下载的PNG图像的字节与使用Python下载的字节不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63694753/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com