< 返回版块

linsinn 发表于 2020-10-08 13:39

Tags:reqwest

以下代码中,rust报错,python正常,但rust中把http改成https就可以。

use reqwest::header::{USER_AGENT, HeaderMap, HeaderValue};
use reqwest::blocking::Client;

fn main() -> Result<(), Box<dyn std::error::Error>>{
	let mut headers = HeaderMap::new();
	headers.insert(USER_AGENT, HeaderValue::from_static("User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36"));
	let client = Client::new();
	let req = client.get("http://www.baidu.com").headers(headers).build()?;
	let rep = client.execute(req)?;
	println!("{:?}", rep.text());
	Ok(())
}
import requests

headers = {
	'User-Agent': 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'
}

print(requests.get("http://www.baidu.com", headers=headers).text)

评论区

写评论
zhangchunzhong 2020-10-08 18:39

[dependencies] reqwest = { version = "0.10", features = ["blocking", "json"] }

zhangchunzhong 2020-10-08 18:38

rustc 1.46.0 上没有问题。

lagudomeze 2020-10-08 17:32

https://docs.rs/reqwest/0.10.8/reqwest/struct.ClientBuilder.html#method.redirect

猜测是重定向的设置不对?

1 共 3 条评论, 1 页