title: Using R to scrape the link address of a downloadable file from a web page?
body: <p>I'm trying to automate a process that involves downloading .zip files from a couple of web pages and extracting the .csvs they contain. The challenge is that the .zip file names, and thus the link addresses, change weekly or annually, depending on the page. Is there a way to scrape the current link addresses from those pages so I can then feed those addresses to a function that downloads the files?</p>
  
  <p>One of the target pages is <a href="http://www.acleddata.com/data/realtime-data-2015/">this one</a>. The file I want to download is the second bullet under the header "2015 Realtime Complete All Africa File"---i.e., the zipped .csv. As I write, that file is labeled "Realtime 2015 All Africa File (updated 11th July 2015)(csv)" on the web page, and the link address that I want is <a href="http://www.acleddata.com/wp-content/uploads/2015/07/ACLED-All-Africa-File_20150101-to-20150711_csv.zip">http://www.acleddata.com/wp-content/uploads/2015/07/ACLED-All-Africa-File_20150101-to-20150711_csv.zip</a>, but that should change later today, because the data are updated each Monday---hence my challenge.</p>
  
  <p>I tried but failed to automate extraction of that .zip file name with 'rvest' and the <a href="http://selectorgadget.com/">selectorgadet</a> extension in Chrome. Here's how that went:</p>
  
  <pre><code>&gt; library(rvest)
  &gt; realtime.page &lt;- "http://www.acleddata.com/data/realtime-data-2015/"
  &gt; realtime.html &lt;- html(realtime.page)
  &gt; realtime.link &lt;- html_node(realtime.html, xpath = "//ul[(((count(preceding-sibling::*) + 1) = 7) and parent::*)]//li+//li//a")
  &gt; realtime.link
  [1] NA
  </code></pre>
  
  <p>The xpath in that call to <code>html_node()</code> came from highlighting just the (csv) portion of the Realtime 2015 All Africa File (updated 11th July 2015)(csv) field in green and then clicking on enough other highlighted bits of the page to eliminate all the yellow and leave only red and green.</p>
  
  <p>Did I make a small mistake in that process, or am I just entirely on the wrong track here? As you can tell, I have zero experience with HTML and web-scraping, so I'd really appreciate some assistance.</p>