Created
November 3, 2024 13:11
-
-
Save limichange/3fa3b96e37f5ad941e6251566d4b02eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use rocket::serde::{Serialize, json::Json}; | |
use reqwest::Client; | |
use scraper::{Html, Selector}; | |
use reqwest::header::{HeaderMap, USER_AGENT}; | |
#[macro_use] extern crate rocket; | |
#[derive(Serialize)] | |
struct ResponseData { | |
message: String, | |
value: i32, | |
} | |
#[derive(Serialize)] | |
struct MetaResponse { | |
metas: Vec<MetaData>, | |
} | |
#[derive(Serialize)] | |
struct MetaData { | |
name: String, | |
content: String, | |
} | |
#[get("/")] | |
fn index() -> &'static str { | |
"Hello, World! Use /json?url=<your_url> to fetch meta information." | |
} | |
#[get("/data")] | |
fn get_data() -> Json<ResponseData> { | |
let response = ResponseData { | |
message: String::from("Hello, World!"), | |
value: 42, | |
}; | |
Json(response) | |
} | |
#[get("/json?<url>")] | |
async fn get_json(url: String) -> Result<Json<MetaResponse>, String> { | |
let client = Client::new(); | |
let mut headers = HeaderMap::new(); | |
headers.insert(USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36".parse().unwrap()); | |
// 发起请求并获取 HTML 内容 | |
match client.get(url).headers(headers).send().await { | |
Ok(response) => { | |
let body = response.text().await.map_err(|e| e.to_string())?; | |
let document = Html::parse_document(&body); | |
let selector = Selector::parse("meta").unwrap(); | |
// 提取所有 meta 标签的 name 和 content 属性 | |
let mut metas: Vec<MetaData> = Vec::new(); | |
for meta in document.select(&selector) { | |
if let Some(name) = meta.value().attr("name") { | |
if let Some(content) = meta.value().attr("content") { | |
metas.push(MetaData { | |
name: name.to_string(), | |
content: content.to_string(), | |
}); | |
} | |
} | |
} | |
let meta_response = MetaResponse { metas }; | |
Ok(Json(meta_response)) | |
}, | |
Err(e) => Err(e.to_string()), | |
} | |
} | |
#[launch] | |
fn rocket() -> _ { | |
rocket::build().mount("/", routes![index, get_data, get_json]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment