<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/scraping-responsibly-with-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://stevenmortimer.com/scraping-responsibly-with-r"> Blog-rss on stevenmortimer.com</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img src="https://i0.wp.com/stevenmortimer.com/blog/scraping-responsibly-with-r/always-scrape-responsibly.jpg?w=456&ssl=1" data-recalc-dims="1"> I recently wrote a blog post <a rel="nofollow" href="https://stevenmortimer.com/most-starred-r-packages-on-github/" target="_blank">here</a> comparing the number of CRAN downloads an R package gets relative to its number of stars on GitHub. What I didn’t really think about during my analysis was whether or not scraping CRAN was a violation of its Terms and Conditions. I simply copy and pasted some code from <a rel="nofollow" href="https://www.r-bloggers.com/" target="_blank">R-bloggers</a> that seemed to work and went on my merry way. In hindsight, it would have been better to check whether or not the scraping was allowed and maybe find a better way to get the information I needed. Of course, there was a much easier way to get the CRAN package metadata using the function <code>tools::CRAN_package_db()</code> thanks to a hint from Maëlle Salmon provided in this <a rel="nofollow" href="https://twitter.com/ma_salmon/status/1009525735277678593" target="_blank">tweet</a>.</p>
<div>
<h2>How to Check if Scraping is Permitted</h2>
<p>Also provided by Maëlle’s tweet was the recommendation for using the <strong>robotstxt</strong> package (currently having 27 Stars + one Star that I just added!). It doesn’t seem to be well known as it only has 6,571 total downloads. I’m hoping this post will help spread the word. It’s easy to use! In this case I’ll check whether or not CRAN permits bots on specific resources of the domain.</p>
<p>My other blog post analysis originally started with trying to get a list of all current R packages on CRAN by parsing the HTML from <a rel="nofollow" href="https://cran.rstudio.com/src/contrib" target="_blank">https://cran.rstudio.com/src/contrib</a>. The page looks like this: <img src="https://i0.wp.com/stevenmortimer.com/blog/scraping-responsibly-with-r/cran-screenshot.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The question is whether or not scraping this page is permitted according to the robots.txt file on the <code>cran.rstudio.com</code> domain. This is where the <strong>robotstxt</strong> package can help us out. We can check simply by supplying the domain and path that is used to form the full link we are interested in scraping. If the <code>paths_allowed()</code> function returns <code>TRUE</code> then we should be allowed to scrape, if it returns <code>FALSE</code> then we are not permitted to scrape.</p>
<pre><code>library(robotstxt)

paths_allowed(
  paths  = &quot;/src/contrib&quot;, 
  domain = &quot;cran.rstudio.com&quot;, 
  bot    = &quot;*&quot;
)
#> [1] TRUE</code></pre>
<p>In this case the value that is returned is <code>TRUE</code> meaning that bots are allowed to scrape that particular path. This was how I originally scraped the list of current R packages, even though you don’t really need to do that since there is the wonderful function <code>tools::CRAN_package_db()</code>.</p>
<p>After retrieving the list of packages I decided to scrape details from the DESCRIPTION file of each package. Here is where things get interesting. CRAN’s <a rel="nofollow" href="http://cran.rstudio.com/robots.txt" target="_blank"><code>robots.txt</code></a> file shows that scraping the DESCRIPTION file of each package is not allowed. <img src="https://i0.wp.com/stevenmortimer.com/blog/scraping-responsibly-with-r/robots-screenshot.png?w=456&ssl=1" data-recalc-dims="1"> Furthermore, you can verify this using the <strong>robotstxt</strong> package:</p>
<pre><code>paths_allowed(
  paths = &quot;/web/packages/ggplot2/DESCRIPTION&quot;,
  domain = &quot;cran.r-project.org&quot;, 
  bot    = &quot;*&quot;
)
#> [1] FALSE</code></pre>
<p>However, when I decided to scrape the package metadata I did it by parsing the HTML from the canonical package link that resolves to the <code>index.html</code> page for the package. For example, <a rel="nofollow" href="https://cran.r-project.org/package=ggplot2" target="_blank">https://cran.r-project.org/package=ggplot2</a> resolves to <a rel="nofollow" href="https://cran.r-project.org/web/packages/ggplot2/index.html" target="_blank">https://cran.r-project.org/web/packages/ggplot2/index.html</a>. If you check whether scraping is allowed on this page, the <strong>robotstxt</strong> package says that it is permitted.</p>
<pre><code>paths_allowed(
  paths = &quot;/web/packages/ggplot2/index.html&quot;,
  domain = &quot;cran.r-project.org&quot;, 
  bot    = &quot;*&quot;
)
#> [1] TRUE

paths_allowed(
  paths = &quot;/web/packages/ggplot2&quot;,
  domain = &quot;cran.r-project.org&quot;, 
  bot    = &quot;*&quot;
)
#> [1] TRUE</code></pre>
<p>This is a tricky situation because I can access the same information that is in the DESCRIPTION file just by going to the <code>index.html</code> page for the package where scraping seems to be allowed. In the spirit of respecting CRAN it logically follows that I should not be scraping the package index pages if the individual DESCRIPTION files are off-limits. This is despite there being no formal instruction from the <code>robots.txt</code> file about package index pages. All in all, it was an interesting bit of work and glad that I was able to learn about the <strong>robotstxt</strong> package so I can have it in my toolkit going forward.</p>
<div>
<b>Remember to Always Scrape Responsibly!</b>
</div>
<p> <strong>DISCLAIMER</strong>: I only have a basic understanding of how <code>robots.txt</code> files work based on allowing or disallowing specified paths. I believe in this case CRAN’s <code>robots.txt</code> broadly permitted scraping, but too narrowly disallowed just the DESCRIPTION files. Perhaps this goes back to an older time where those DESCRIPTION files really were the best place for people to start scraping so it made sense to disallow them. Or the reason could be something else entirely.</p>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://stevenmortimer.com/scraping-responsibly-with-r"> Blog-rss on stevenmortimer.com</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/bM1H3rg89Qg" height="1"></a></div>