Web Scraper Task avatar

Web Scraper Task

Try for free

No credit card required

Go to Store
Web Scraper Task

Web Scraper Task

undrtkr984/web-scraper-task
Try for free

No credit card required

You can access the Web Scraper Task programmatically from your own Python applications by using the Apify API. You can also choose the language preference from below. To use the Apify API, you’ll need an Apify account and your API token, found in Integrations settings in Apify Console.

1from apify_client import ApifyClient
2
3# Initialize the ApifyClient with your Apify API token
4# Replace '<YOUR_API_TOKEN>' with your token.
5client = ApifyClient("<YOUR_API_TOKEN>")
6
7# Prepare the Actor input
8run_input = {
9    "runMode": "DEVELOPMENT",
10    "startUrls": [{ "url": "https://crawlee.dev" }],
11    "linkSelector": "a[href]",
12    "globs": [{ "glob": "https://crawlee.dev/*/*" }],
13    "pseudoUrls": [],
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("undrtkr984/web-scraper-task").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

Web Scraper Task API in Python

The Apify API client for Python is the official library that allows you to use Web Scraper Task API in Python, providing convenience functions and automatic retries on errors.

Install the apify-client

pip install apify-client

Other API clients include:

Developer
Maintained by Community

Actor Metrics

  • 4 monthly users

  • 1 star

  • >99% runs succeeded

  • Created in Jan 2023

  • Modified 2 years ago

Categories