te2efwdfg avatar
te2efwdfg
Try for free

No credit card required

View all Actors
te2efwdfg

te2efwdfg

astounding_lever/wb-spr-eg3g
Try for free

No credit card required

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    "runMode": "DEVELOPMENT",
9    "startUrls": [{ "url": "https://crawlee.dev" }],
10    "linkSelector": "a[href]",
11    "globs": [{ "glob": "https://crawlee.dev/*/*" }],
12    "pseudoUrls": [],
13    "excludes": [{ "glob": "/**/*.{png,jpg,jpeg,pdf}" }],
14    "pageFunction": """// The function accepts a single argument: the \"context\" object.
15// For a complete list of its properties and functions,
16// see https://apify.com/apify/web-scraper#page-function 
17async function pageFunction(context) {
18    // This statement works as a breakpoint when you're trying to debug your code. Works only with Run mode: DEVELOPMENT!
19    // debugger; 
20
21    // jQuery is handy for finding DOM elements and extracting data from them.
22    // To use it, make sure to enable the \"Inject jQuery\" option.
23    const $ = context.jQuery;
24    const pageTitle = $('title').first().text();
25    const h1 = $('h1').first().text();
26    const first_h2 = $('h2').first().text();
27    const random_text_from_the_page = $('p').first().text();
28
29
30    // Print some information to actor log
31    context.log.info(`URL: ${context.request.url}, TITLE: ${pageTitle}`);
32
33    // Manually add a new page to the queue for scraping.
34   await context.enqueueRequest({ url: 'http://www.example.com' });
35
36    // Return an object with the data extracted from the page.
37    // It will be stored to the resulting dataset.
38    return {
39        url: context.request.url,
40        pageTitle,
41        h1,
42        first_h2,
43        random_text_from_the_page
44    };
45}""",
46    "proxyConfiguration": { "useApifyProxy": True },
47    "initialCookies": [],
48    "waitUntil": ["networkidle2"],
49    "preNavigationHooks": """// We need to return array of (possibly async) functions here.
50// The functions accept two arguments: the \"crawlingContext\" object
51// and \"gotoOptions\".
52[
53    async (crawlingContext, gotoOptions) => {
54        // ...
55    },
56]
57""",
58    "postNavigationHooks": """// We need to return array of (possibly async) functions here.
59// The functions accept a single argument: the \"crawlingContext\" object.
60[
61    async (crawlingContext) => {
62        // ...
63    },
64]""",
65    "breakpointLocation": "NONE",
66    "customData": {},
67}
68
69# Run the Actor and wait for it to finish
70run = client.actor("astounding_lever/wb-spr-eg3g").call(run_input=run_input)
71
72# Fetch and print Actor results from the run's dataset (if there are any)
73print("💾 Check your data here: https://console.apify.com/storage/datasets/" + run["defaultDatasetId"])
74for item in client.dataset(run["defaultDatasetId"]).iterate_items():
75    print(item)
76
77# 📚 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
  • 0 stars
  • 100.0% runs succeeded
  • Created in Jan 2024
  • Modified 5 months ago
Categories