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

问题描述

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

例如:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

如果我将结果打印为字节数组(let content = reqwest::get("https://www.google.es/images/searchBox/desktop_searchBox_sprites302_hr.png").await?; );

println!("{:?}",content.text().await?.as_bytes()

但是,使用Python请求的结果是:

[ 191,189,80,78,71,13,10,26,73,72,68,82,40,8,3,17,191,102,108,76,84,69,...]

在Rust版本中,我看到了很多[137,153,248,...] 。此序列在整个数组中重复很多,但是在Python中根本没有出现。

我在Rust中做错什么了?

解决方法

我看到很多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,49,48,44,52,28,8,3,11,99,66,f8,6c,4c,54,45,9f,...