title: Why is my R webscraper slower than webmonitor apps?
body: <p>Every day around 12:30 the Swiss government publishes corona data on a website. I run a scraper that is supposed to notify a slack-channel as soon as the numbers are published.
  However, every day my scraper is a few minutes slower than the webmonitor apps my colleagues use. I don't know how to make it even faster.
  At this point it runs on a repeat loop that scrapes the website every 0-2 seconds and I start it around 11:00 with a Linux crontab. Any suggestions?</p>
  <pre><code>#Packages
  
  if (!require('lubridate')) {install.packages('lubridate')};library('lubridate')
  if (!require('magrittr')) {install.packages('magrittr')};library('magrittr')
  if (!require('rvest')) {install.packages('rvest')};library('rvest')
  if (!require('slackr')) {install.packages('slackr')};library('slackr')
  if (!require('XML')) {install.packages('XML')};library('XML')
  
  
  #Basics
  linux &lt;- &quot;/home/pi/R/projects&quot;
  setwd(linux)
  slackr_setup(config_file = &quot;.coronapush&quot;)
  y &lt;- 1
  
  #scraping
  bag_alt &lt;- xml2::read_html('https://www.bag.admin.ch/bag/de/home/krankheiten/ausbrueche-epidemien-pandemien/aktuelle-ausbrueche-epidemien/novel-cov/situation-schweiz-und-international.html')
  anzahl_faelle_alt &lt;- bag_alt %&gt;% 
    html_nodes('#2030838475+ .mod-table tr:nth-child(1) td:nth-child(2)') %&gt;% 
    html_text()
  anzahl_faelle_alt &lt;- gsub(&quot;[[:space:]]&quot;, &quot;&quot;, anzahl_faelle_alt) %&gt;%
    as.integer()
  anzahl_tote_alt &lt;- bag_alt %&gt;%
    html_nodes('#2030838475+ .mod-table tr:nth-child(3) td:nth-child(2)') %&gt;% 
    html_text()
  anzahl_tote_alt &lt;- gsub(&quot;[[:space:]]&quot;, &quot;&quot;, anzahl_tote_alt) %&gt;%
    as.integer()
  anzahl_tests_alt &lt;- bag_alt %&gt;%
    html_nodes('tr:nth-child(4) td:nth-child(2)')%&gt;%
    html_text()
  anzahl_tests_alt &lt;- gsub(&quot;[[:space:]]&quot;, &quot;&quot;, anzahl_tests_alt) %&gt;%
    as.integer()
  
  #prepare loop
  tests_gemacht &lt;- F
  faelle_gemacht &lt;- F
  tote_gemacht &lt;- F
  y &lt;- 1
  
  #Loop
  
  repeat {
    bag_neu &lt;- xml2::read_html('https://www.bag.admin.ch/bag/de/home/krankheiten/ausbrueche-epidemien-pandemien/aktuelle-ausbrueche-epidemien/novel-cov/situation-schweiz-und-international.html')  
      anzahl_faelle_neu &lt;- bag_neu %&gt;% 
        html_nodes('#2030838475+ .mod-table tr:nth-child(1) td:nth-child(2)') %&gt;% 
        html_text() 
      anzahl_faelle_neu &lt;- gsub(&quot;[[:space:]]&quot;, &quot;&quot;, anzahl_faelle_neu) %&gt;%
        as.integer()
      
      anzahl_tote_neu &lt;- bag_neu %&gt;%
        html_nodes('#2030838475+ .mod-table tr:nth-child(3) td:nth-child(2)') %&gt;% 
        html_text() 
      anzahl_tote_neu &lt;- gsub(&quot;[[:space:]]&quot;, &quot;&quot;, anzahl_tote_neu) %&gt;%
        as.integer()
  
      anzahl_tests_neu &lt;- bag_neu %&gt;%
        html_nodes('tr:nth-child(4) td:nth-child(2)')%&gt;%
        html_text()
      anzahl_tests_neu &lt;- gsub(&quot;[[:space:]]&quot;, &quot;&quot;, anzahl_tests_neu) %&gt;%
        as.integer()
      
      if(anzahl_faelle_alt != anzahl_faelle_neu) {
        ansteckungen &lt;- paste0(&quot;Neue BAG-Zahlen: &quot;, anzahl_faelle_neu, &quot; Ansteckungen,&quot;)
        slackr_bot(ansteckungen)
        faelle_gemacht &lt;- T
        }
      if(anzahl_tests_alt != anzahl_tests_neu) { 
        anzahl_tests_neu_formatiert &lt;- format(anzahl_tests_neu, big.mark = &quot;'&quot;)
        tests &lt;- paste0(anzahl_tests_neu_formatiert, &quot; Tests&quot;)
          slackr_bot(tests)
          tests_gemacht &lt;- T
          }
     
      if(tests_gemacht == T &amp;&amp; faelle_gemacht == T) {
        positivitaet &lt;- anzahl_faelle_neu/anzahl_tests_neu*100
        positivitaet&lt;-  round(positivitaet,1) %&gt;%
          as.character()
        positivitaet &lt;- paste0(positivitaet, &quot;% Positivität&quot;)
        slackr_bot(positivitaet)
      }
      if(anzahl_tote_alt != anzahl_tote_neu) { 
        tote &lt;- paste0(anzahl_tote_neu, &quot; Tote&quot;)
        slackr_bot(tote)
        tote_gemacht &lt;- T
      }
      
      if(tests_gemacht == T &amp;&amp; faelle_gemacht == T &amp;&amp; tote_gemacht == F){
        tote &lt;- paste0(anzahl_tote_neu, &quot; Tote&quot;)
        slackr_bot(tote)
        tote_gemacht &lt;- T
      }
  y &lt;-y+1    
  if (faelle_gemacht == T &amp;&amp; tote_gemacht == T &amp;&amp; tests_gemacht == T){
    quit()
  }
  if (y == 15000){
    quit()
  }
  x &lt;- runif(1,0,2)
  Sys.sleep(x)
  print(y)
  }
  </code></pre>