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