mirror of https://github.com/KaiserY/trpl-zh-cn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
22 lines
590 B
22 lines
590 B
8 months ago
|
extern crate trpl; // required for mdbook test
|
||
|
|
||
|
use trpl::Html;
|
||
|
|
||
|
// ANCHOR: main
|
||
|
async fn main() {
|
||
|
let args: Vec<String> = std::env::args().collect();
|
||
|
let url = &args[1];
|
||
|
match page_title(url).await {
|
||
|
Some(title) => println!("The title for {url} was {title}"),
|
||
|
None => println!("{url} had no title"),
|
||
|
}
|
||
|
}
|
||
|
// ANCHOR_END: main
|
||
|
|
||
|
async fn page_title(url: &str) -> Option<String> {
|
||
|
let response_text = trpl::get(url).await.text().await;
|
||
|
Html::parse(&response_text)
|
||
|
.select_first("title")
|
||
|
.map(|title_element| title_element.inner_html())
|
||
|
}
|