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

1from apify_client import ApifyClient
2
3# Initialize the ApifyClient with your Apify API token
4client = ApifyClient("<YOUR_API_TOKEN>")
5
6# Prepare the Actor input
7run_input = {
8    "url": "https://apify.com",
9    "page_function": """use serde_json::{Value,json};
10use scraper::{Html, Selector};
11
12fn selector_to_text(document: &Html, selector: &str) -> Option<String> {
13    document
14        .select(&Selector::parse(selector).unwrap())
15        .next()
16        .map(|el| el.text().next().unwrap().into() )
17}
18
19#[no_mangle]
20pub fn page_function (document: &Html) -> Value { 
21    println!(\"page_function starting\");
22
23    let title = selector_to_text(&document, \"title\");
24    println!(\"extracted title: {:?}\", title);
25
26    let header = selector_to_text(&document, \"h1\");
27    println!(\"extracted header: {:?}\", header);
28
29    let companies_using_apify = document
30        .select(&Selector::parse(\".Logos__container\").unwrap())
31        .next().unwrap()
32        .select(&Selector::parse(\"img\").unwrap())
33        .map(|el| el.value().attr(\"alt\").unwrap().to_string())
34        .collect::<Vec<String>>();
35
36    println!(\"extracted companies_using_apify: {:?}\", companies_using_apify);
37
38    let output = json!({
39        \"title\": title,
40        \"header\": header,
41        \"companies_using_apify\": companies_using_apify,
42    });
43    println!(\"inside pageFunction output: {:?}\", output);
44    output
45}""",
46}
47
48# Run the Actor and wait for it to finish
49run = client.actor("lukaskrivka/rust-input-function-example").call(run_input=run_input)
50
51# Fetch and print Actor results from the run's dataset (if there are any)
52print("💾 Check your data here: https://console.apify.com/storage/datasets/" + run["defaultDatasetId"])
53for item in client.dataset(run["defaultDatasetId"]).iterate_items():
54    print(item)
55
56# 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/python/docs/quick-start
Developer
Maintained by Community
Actor metrics
  • 1 monthly user
  • 2 stars
  • 96.6% runs succeeded
  • Created in Dec 2023
  • Modified 8 months ago