use serde::Deserialize;
use serde_json::json;
use webscrape_ai::{Client, SmartScraperRequest};
#[derive(Debug, Deserialize)]
struct Page {
stories: Vec<Story>,
}
#[derive(Debug, Deserialize)]
struct Story {
title: Option<String>,
url: Option<String>,
score: Option<i64>,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::from_env()?;
let resp = client
.smartscraper(
SmartScraperRequest::new(
"https://news.ycombinator.com",
"Extract the front-page stories with title, url, and score.",
)
.output_schema(json!({
"type": "object",
"properties": {
"stories": {
"type": "array",
"items": {
"type": "object",
"properties": {
"title": { "type": "string" },
"url": { "type": "string" },
"score": { "type": "integer" }
}
}
}
}
})),
)
.await?;
println!("credits_used={:?}", resp.credits_used);
let page: Page = resp.data.result_as()?;
for story in page.stories.iter().take(10) {
println!("[{:?}] {:?} ({:?})", story.score, story.title, story.url);
}
Ok(())
}