
[dependencies]
# json 序列化 反序列化
serde_json = "1.0"
# 时间工具包
chrono = {version = "0.4.31", features = ["serde"] }
# http 客户端 爬取相关数据使用
reqwest = {version = "0.11.22", features = ["json", "stream"] }
# reqwest 增强
futures-util = "0.3.29"
futures-core = "0.3.29"
# rust知名异步框架 实现多线程等
tokio = { version = "1.34.0", features = ["rt", "rt-multi-thread", "macros"] }
# 宏 可以静态分配中调用方法
lazy_static = "1.4.0"
# 对爬取的html进行解析处理
htmler = "0.1.1"
# 轻松实现错误处理
anyhow = "1.0.75"
# 异步方法测试宏
actix-rt = "2.9.0"
use lazy_static::lazy_static;
use reqwest::Client;
use htmler::Selector;
#[tokio::main]
async fn main() {
}
lazy_static!{
/// bing 必应图片第三方网站
static ref BING_URL: String = "https://peapix.com/bing/cn".to_string();
static ref BING_PAGE_NUMBERS_SELECTOR: Selector = Selector::parse(r#"a[class="page-link"]"#).unwrap();
}
/// `get_bing_total_page` 获取bing网页中总页数
///
/// # Examples
///
/// ```
/// let total = get_bing_total_page();
///
/// assert_eq!(Some(50), total);
/// ```
async fn get_bing_total_page() -> Option<i32>{
let client = Client::new();
if let Ok(res) = client.get(BING_URL.clone()).send().await{
let html_dom = res.text().await.unwrap();
let html = htmler::Html::parse_fragment(&html_dom);
if let Some(node) = html.select(&BING_PAGE_NUMBERS_SELECTOR).last(){
return Some(node.inner_html().parse::<i32>().unwrap());
}
}
None
}
#[cfg(test)]
mod tests{
use crate::get_bing_total_page;
#[actix_rt::test]
async fn get_bing_total_page_test(){
let total = get_bing_total_page().await;
assert_eq!(Some(50), total)
}
}
