Using Robotstxt"

Description

The package provides a simple 'robotstxt' class and accompanying methods to parse and check 'robots.txt' files. Data fields are provided as data frames and vectors. Permissions can be checked by providing path character vectors and optional bot names.

Robots.txt files

Robots.txt files are a way to kindly ask webbots, spiders, crawlers, wanderers and the like to access or not access certain parts of a webpage. The de facto 'standard' never made it beyond a informal "Network Working Group INTERNET DRAFT". Nonetheless, the use of robots.txt files is widespread (e.g. https://en.wikipedia.org/robots.txt, https://www.google.com/robots.txt) and bots from Google, Yahoo and the like will adhere to the rules defined in robots.txt files - although, their interpretation of those rules might differ (e.g. rules for googlebot ).

As the name of the files already suggests robots.txt files are plain text and always found at the root of a domain. The syntax of the files in essence follows a fieldname: value scheme with optional preceding user-agent: ... lines to indicate the scope of the following rule block. Blocks are separated by blank lines and the omission of a user-agent field (which directly corresponds to the HTTP user-agent field) is seen as referring to all bots. # serves to comment lines and parts of lines. Everything after # until the end of line is regarded a comment. Possible field names are: user-agent, disallow, allow, crawl-delay, sitemap, and host.

Let us have an example file to get an idea how a robots.txt file might look like. The file below starts with a comment line followed by a line disallowing access to any content -- everything that is contained in root ("/") -- for all bots. The next block concerns GoodBot and NiceBot. Those two get the previous permissions lifted by being disallowed nothing. The third block is for PrettyBot. PrettyBot likes shiny stuff and therefor gets a special permission for everything contained in the "/shinystuff/" folder while all other restrictions still hold. In the last block all bots are asked to pause at least 5 seconds between two visits.

# this is a comment 
# a made up example of an robots.txt file

Disallow: /

User-agent: GoodBot # another comment
User-agent: NiceBot
Disallow: 

User-agent: PrettyBot
Allow: /shinystuff/

Crawl-Delay: 5

For more information have a look at: http://www.robotstxt.org/norobots-rfc.txt, where the robots.txt file 'standard' is described formally. Valuable introductions can be found at http://www.robotstxt.org/robotstxt.html as well as at https://en.wikipedia.org/wiki/Robots_exclusion_standard - of cause.

Fast food usage for the uninterested

library(robotstxt)
paths_allowed("http://google.com/")
paths_allowed("http://google.com/search")

Example Usage

First, let us load the package. In addition we load the dplyr package to be able to use the magrittr pipe operator %>% and some easy to read and remember data manipulation functions.

library(robotstxt)
library(dplyr)

object oriented style

The first step is to create an instance of the robotstxt class provided by the package. The instance has to be initiated via providing either domain or the actual text of the robots.txt file. If only the domain is provided, the robots.txt file will be downloaded automatically. Have a look at ?robotstxt for descriptions of all data fields and methods as well as their parameters.

rtxt <- 
  robotstxt(
    domain = "wikipedia.org", 
    text   = robotstxt:::rt_get_rtxt("robots_wikipedia.txt")
  )
rtxt <- robotstxt(domain="wikipedia.org")

rtxt is of class robotstxt.

class(rtxt)

Printing the object lets us glance at all data fields and methods in rtxt - we have access to the text as well as all common fields. Non-standard fields are collected in other.

rtxt

Checking permissions works via rtxt's check method by providing one or more paths. If no bot name is provided "*" - meaning any bot - is assumed.

# checking for access permissions
rtxt$check(paths = c("/","api/"), bot = "*")
rtxt$check(paths = c("/","api/"), bot = "Orthogaffe")
rtxt$check(paths = c("/","api/"), bot = "Mediapartners-Google*  ")

functional style

While working with the robotstxt class is recommended the checking can be done with functions only as well. In the following we (1) download the robots.txt file; (2) parse it and (3) check permissions.

r_text <- robotstxt:::rt_get_rtxt("robots_new_york_times.txt")
r_text        <- get_robotstxt("nytimes.com") 
r_parsed <- parse_robotstxt(r_text)
r_parsed
paths_allowed(
  paths  = c("images/","/search"), 
  domain = c("wikipedia.org", "google.com"),
  bot    = "Orthogaffe"
)


Try the robotstxt package in your browser

Any scripts or data that you put into this service are public.

robotstxt documentation built on Sept. 4, 2020, 1:08 a.m.