samclarke / robots-parser

NodeJS robots.txt parser with support for wildcard (*) matching.
MIT License
150 stars 19 forks source link
javascript nodejs rfc-9309 robots-exclusion-standard robots-parser robots-txt user-agent

Robots Parser NPM downloads DeepScan grade GitHub license Coverage Status

A robots.txt parser which aims to be complaint with the RFC 9309 specification.

The parser currently supports:

Installation

Via NPM:

npm install robots-parser

or via Yarn:

yarn add robots-parser

Usage

var robotsParser = require('robots-parser');

var robots = robotsParser('http://www.example.com/robots.txt', [
    'User-agent: *',
    'Disallow: /dir/',
    'Disallow: /test.html',
    'Allow: /dir/test.html',
    'Allow: /test.html',
    'Crawl-delay: 1',
    'Sitemap: http://example.com/sitemap.xml',
    'Host: example.com'
].join('\n'));

robots.isAllowed('http://www.example.com/test.html', 'Sams-Bot/1.0'); // true
robots.isAllowed('http://www.example.com/dir/test.html', 'Sams-Bot/1.0'); // true
robots.isDisallowed('http://www.example.com/dir/test2.html', 'Sams-Bot/1.0'); // true
robots.isExplicitlyDisallowed('http://www.example.com/dir/test2.html', 'Sams-Bot/1.0'); // false
robots.getCrawlDelay('Sams-Bot/1.0'); // 1
robots.getSitemaps(); // ['http://example.com/sitemap.xml']
robots.getPreferredHost(); // example.com

isAllowed(url, [ua])

boolean or undefined

Returns true if crawling the specified URL is allowed for the specified user-agent.

This will return undefined if the URL isn't valid for this robots.txt.

isDisallowed(url, [ua])

boolean or undefined

Returns true if crawling the specified URL is not allowed for the specified user-agent.

This will return undefined if the URL isn't valid for this robots.txt.

isExplicitlyDisallowed(url, ua)

boolean or undefined

[!CAUTION] This is not part of the robots.txt specification and should only be used with the websites owners permission. This method is only intended for special purposes where a user-agent shouldn't fallback to matching against global (*) rules.

An example of this behaviour is Google AdsBot which must be explicitly excluded. This is done with the website owners permission.

Returns trues if explicitly disallowed for the specified user agent (User Agent wildcards are discarded).

This will return undefined if the URL is not valid for this robots.txt file.

getMatchingLineNumber(url, [ua])

number or undefined

Returns the line number of the matching directive for the specified URL and user-agent if any.

Line numbers start at 1 and go up (1-based indexing).

Returns -1 if there is no matching directive. If a rule is manually added without a lineNumber then this will return undefined for that rule.

getCrawlDelay([ua])

number or undefined

Returns the number of seconds the specified user-agent should wait between requests.

Returns undefined if no crawl delay has been specified for this user-agent.

getSitemaps()

array

Returns an array of sitemap URLs specified by the sitemap: directive.

getPreferredHost()

string or null

Returns the preferred host name specified by the host: directive or null if there isn't one.

Changes

Version 3.0.1

Version 3.0.0

Version 2.4.0:

Version 2.3.0:

Version 2.2.0:

Version 2.1.1:

Version 2.1.0:

Version 2.0.0:

This release is not 100% backwards compatible as it now uses the new URL APIs which are not supported in Node < 7.

Version 1.0.2:

Version 1.0.1:

Version 1.0.0:

License

The MIT License (MIT)

Copyright (c) 2014 Sam Clarke

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.