Rust Input Function Example avatar
Rust Input Function Example
Try for free

No credit card required

View all Actors
Rust Input Function Example

Rust Input Function Example

lukaskrivka/rust-input-function-example
Try for free

No credit card required

Dynamically compile and run input-provided page function. Like Cheerio Scraper but in Rust.

The code examples below show how to run the Actor and get its results. To run the code, you need to have an Apify account. Replace <YOUR_API_TOKEN> in the code with your API token, which you can find under Settings > Integrations in Apify Console. Learn more

1# Set API token
2API_TOKEN=<YOUR_API_TOKEN>
3
4# Prepare Actor input
5cat > input.json <<'EOF'
6{
7  "url": "https://apify.com",
8  "page_function": "use serde_json::{Value,json};\nuse scraper::{Html, Selector};\n\nfn selector_to_text(document: &Html, selector: &str) -> Option<String> {\n    document\n        .select(&Selector::parse(selector).unwrap())\n        .next()\n        .map(|el| el.text().next().unwrap().into() )\n}\n\n#[no_mangle]\npub fn page_function (document: &Html) -> Value { \n    println!(\"page_function starting\");\n\n    let title = selector_to_text(&document, \"title\");\n    println!(\"extracted title: {:?}\", title);\n\n    let header = selector_to_text(&document, \"h1\");\n    println!(\"extracted header: {:?}\", header);\n\n    let companies_using_apify = document\n        .select(&Selector::parse(\".Logos__container\").unwrap())\n        .next().unwrap()\n        .select(&Selector::parse(\"img\").unwrap())\n        .map(|el| el.value().attr(\"alt\").unwrap().to_string())\n        .collect::<Vec<String>>();\n\n    println!(\"extracted companies_using_apify: {:?}\", companies_using_apify);\n\n    let output = json!({\n        \"title\": title,\n        \"header\": header,\n        \"companies_using_apify\": companies_using_apify,\n    });\n    println!(\"inside pageFunction output: {:?}\", output);\n    output\n}"
9}
10EOF
11
12# Run the Actor using an HTTP API
13# See the full API reference at https://docs.apify.com/api/v2
14curl "https://api.apify.com/v2/acts/lukaskrivka~rust-input-function-example/runs?token=$API_TOKEN" \
15  -X POST \
16  -d @input.json \
17  -H 'Content-Type: application/json'
Developer
Maintained by Community
Actor metrics
  • 1 monthly user
  • 2 stars
  • 96.6% runs succeeded
  • Created in Dec 2023
  • Modified 8 months ago