Trouble downloading xlsx file from website - Scraping
<p>I'm trying to write some code which download the two latest publications of the Outage Weeks found at the  bottom of <a href="http://www.eirgridgroup.com/customer-and-industry/general-customer-information/outage-information/" rel="nofollow">http://www.eirgridgroup.com/customer-and-industry/general-customer-information/outage-information/</a></p>

<p>It's xlsx-files, which I'm going to load into Excel afterwards.
It doesn't matter which programming language the code is written in. </p>

<p>My first idea was to use the direct url's, like <a href="http://www.eirgridgroup.com/site-files/library/EirGrid/Outage-Weeks_36(2016)-51(2016)_31%20August.xlsx" rel="nofollow">http://www.eirgridgroup.com/site-files/library/EirGrid/Outage-Weeks_36(2016)-51(2016)_31%20August.xlsx</a>
, and then make some code which guesses the url of the two latest publications.
But I have noticed some inconsistencies in the url names, so that solution wouldn't work. </p>

<p>Instead it might be solution to scrape the website and use the XPath to download the files. I found out that the two latest publications always have the following XPaths:</p>

<pre><code>/html/body/div[3]/div[3]/div/div/p[5]/a
/html/body/div[3]/div[3]/div/div/p[6]/a
</code></pre>

<p>This is where I need help. I'm new to both XPath and Web Scraping. I have tried stuff like this in Python</p>

<pre><code>from lxml import html
import requests

page = requests.get('http://www.eirgridgroup.com/customer-and-industry/general-customer-information/outage-information/')
tree = html.fromstring(page.content)

v = tree.xpath('/html/body/div[3]/div[3]/div/div/p[5]/a')
</code></pre>

<p>But v seems to be empty.</p>

<p>Any ideas would be greatly appreciated! </p>

<p>Just use <em>contains</em> to find the <em>hrefs</em> and slice the first two:</p>

<pre><code> tree.xpath('//p/a[contains(@href, "/site-files/library/EirGrid/Outage-Weeks")]/@href')[:2]
</code></pre>

<p>Or doing it all with the xpath using <code>[position() &lt; 3]</code>:</p>

<pre><code>tree.xpath'(//p/a[contains(@href, "site-files/library/EirGrid/Outage-Weeks")])[position() &lt; 3]/@href')
</code></pre>

<p>The files are ordered from latest to oldest so getting the first two gives you the two newest.</p>

<p>To download the files you just need to join each href to the base url and write the content to a file:</p>

<pre><code>from lxml import html
import requests
import os
from urlparse import urljoin # from urllib.parse import urljoin


page = requests.get('http://www.eirgridgroup.com/customer-and-industry/general-customer-information/outage-information/')
tree = html.fromstring(page.content)

v = tree.xpath('(//p/a[contains(@href, "/site-files/library/EirGrid/Outage-Weeks")])[position() &lt; 3]/@href')
for href in v:
    # os.path.basename(href) -&gt; Outage-Weeks_35(2016)-50(2016).xlsx 
    with open(os.path.basename(href), "wb") as f:
        f.write(requests.get(urljoin("http://www.eirgridgroup.com", link)).content)
</code></pre>

python vba xpath import web-scraping