1from apify_client import ApifyClient
2
3
4
5client = ApifyClient("<YOUR_API_TOKEN>")
6
7
8run_input = {
9 "url": "https://apify.com",
10 "page_function": """use serde_json::{Value,json};
11use scraper::{Html, Selector};
12
13fn selector_to_text(document: &Html, selector: &str) -> Option<String> {
14 document
15 .select(&Selector::parse(selector).unwrap())
16 .next()
17 .map(|el| el.text().next().unwrap().into() )
18}
19
20#[no_mangle]
21pub fn page_function (document: &Html) -> Value {
22 println!(\"page_function starting\");
23
24 let title = selector_to_text(&document, \"title\");
25 println!(\"extracted title: {:?}\", title);
26
27 let header = selector_to_text(&document, \"h1\");
28 println!(\"extracted header: {:?}\", header);
29
30 let companies_using_apify = document
31 .select(&Selector::parse(\".Logos__container\").unwrap())
32 .next().unwrap()
33 .select(&Selector::parse(\"img\").unwrap())
34 .map(|el| el.value().attr(\"alt\").unwrap().to_string())
35 .collect::<Vec<String>>();
36
37 println!(\"extracted companies_using_apify: {:?}\", companies_using_apify);
38
39 let output = json!({
40 \"title\": title,
41 \"header\": header,
42 \"companies_using_apify\": companies_using_apify,
43 });
44 println!(\"inside pageFunction output: {:?}\", output);
45 output
46}""",
47}
48
49
50run = client.actor("lukaskrivka/rust-input-function-example").call(run_input=run_input)
51
52
53print("💾 Check your data here: https://console.apify.com/storage/datasets/" + run["defaultDatasetId"])
54for item in client.dataset(run["defaultDatasetId"]).iterate_items():
55 print(item)
56
57