got-scraping

repository·master·Indexed 20 days ago

https://github.com/apify/got-scraping

An extension of the 'got' HTTP client designed for web scraping. It emulates browser-like requests by automatically generating realistic headers, managing TLS fingerprints, and handling proxy protocols to help scrapers blend in with legitimate web traffic. Note: This package is End-of-Life (EOL); for new projects, the use of 'impit' is recommended.

Tokens
6K
Snippets
23
Records
28
Agent score
70%

What's inside got-scraping

  1. Import got-scraping in ESM or CommonJS

    master

    The module is ESM only. You must use import expressions or the import() method.

    If your project is ESM:

    import { gotScraping } from 'got-scraping';

    If you are using CommonJS and cannot migrate to ESM, import it within an async context using dynamic imports:

    let gotScraping;
    
    async function fetchWithGotScraping(url) {
        gotScraping ??= (await import('got-scraping')).gotScraping;
    
        return gotScraping.get(url);
    }

    Requirement: Node.js >=16 is required.

  2. Troubleshoot TLS connection errors

    master

    If you encounter the error: RequestError: Client network socket disconnected before secure TLS connection was established

    This often means the server does not support the provided TLS settings. To resolve this, try changing the ciphers parameter to either undefined or a custom value.

  3. Override request headers

    master

    To manually override the automatically generated browser-like headers, pass a headers object in the request options. This is useful for adding specific headers like referer which might be necessary for certain sites.

    const response = await gotScraping({
        url: 'https://apify.com/',
        headers: {
            'user-agent': 'test',
        },
    });
  4. Use JSON mode

    master

    You can request JSON responses by setting responseType: 'json'.

    Note: Header generation is optimized for HTML content types. When using JSON mode, you may need to manually adjust headers to better match browser behavior for API requests.

    const response = await gotScraping({
        responseType: 'json',
        url: 'https://api.apify.com/v2/browser-info',
    });
  5. Configure header generation with headerGeneratorOptions

    master

    You can customize the browser-like headers generated by the package using headerGeneratorOptions. This allows you to specify target browsers, devices, locales, and operating systems.

    Options include:

    • browsers: Array of browser objects (e.g., { name: 'chrome', minVersion: 87, maxVersion: 89 })
    • devices: Array of device types (e.g., ['desktop'])
    • locales: Array of locales (e.g., ['de-DE', 'en-US'])
    • operatingSystems: Array of operating systems (e.g., ['windows', 'linux'])
    const response = await gotScraping({
        url: 'https://api.apify.com/v2/browser-info',
        headerGeneratorOptions:{
            browsers: [
                {
                    name: 'chrome',
                    minVersion: 87,
                    maxVersion: 89
                }
            ],
            devices: ['desktop'],
            locales: ['de-DE', 'en-US'],
            operatingSystems: ['windows', 'linux'],
        }
    });
  6. Use the gotScraping API

    master

    The gotScraping instance is built using got.extend(), meaning it supports all standard got features. It is designed to send browser-like requests out of the box to help blend in with website traffic.

    import { gotScraping } from 'got-scraping';
    
    gotScraping
        .get('https://apify.com')
        .then( ({ body }) => console.log(body));
  7. Manage request sessions with sessionToken

    master

    The sessionToken option is a non-primitive unique object used to describe the current session.

    • If undefined (default), new headers are generated for every request.
    • If a sessionToken is provided, headers generated with that token will remain consistent across requests.
  8. Configure pagination options

    master

    When using gotScraping.paginate, you provide an object of type ExtendedPaginationOptions.

    Key properties include:

    • pagination: An object of type ExtendedPaginationOptions<ElementType, BodyType>.
    • paginate: A function (data: PaginateData<BodyType, ElementType>) => OptionsInit | false used to determine the next request parameters based on the current page's data. If it returns false, pagination stops.
    • countLimit: (Inherited from PaginationOptions) used to limit the number of items processed.
  9. Configure proxyUrl

    master

    Provide a proxyUrl string to route requests through an HTTP, HTTPS, or HTTP/2 based proxy. Got Scraping automatically detects the protocol supported by the proxy server.

    import { gotScraping } from 'got-scraping';
    
    gotScraping
        .get({
            url: 'https://apify.com',
            proxyUrl: 'http://usernamed:password@myproxy.com:1234',
        })
        .then(({ body }) => console.log(body));
  10. Use sessionDataHook to manage session data

    master

    The sessionDataHook is a middleware hook used to associate session-specific data with a request's context. It uses the sessionToken found in options.context as a key to retrieve or initialize a data object from an internal WeakMap.

    When the hook is executed, it attaches a sessionData object to options.context. This allows you to persist and retrieve state (like cookies or proxy settings) across multiple requests that share the same sessionToken.

    import { sessionDataHook } from 'got-scraping';
    
    // Example usage within a got request configuration
    const response = await got({
        url: 'https://example.com',
        context: {
            sessionToken: 'unique-session-id-123',
            // sessionData will be automatically attached/retrieved by the hook
        },
        hooks: {
            beforeRequest: [sessionDataHook]
        }
    });