title: Download .xls files from a webpage using Python and BeautifulSoup
body: <p>I want to download all the <code>.xls</code> or <code>.xlsx</code> or <code>.csv</code> from this website into a specified folder.</p>
  
  <pre><code>https://www.rbi.org.in/Scripts/bs_viewcontent.aspx?Id=2009
  </code></pre>
  
  <p>I have looked into mechanize, beautiful soup, urllib2 etc. Mechanize does not work in Python 3, urllib2 also had problems with Python 3, I looked for workaround but I couldn't. So, I am currently trying to make it work using Beautiful Soup.</p>
  
  <p>I found some example code and attempted to modify it to suit my problem, as follows - </p>
  
  <pre><code>from bs4 import BeautifulSoup
  # Python 3.x
  from urllib.request import urlopen, urlretrieve, quote
  from urllib.parse import urljoin
  
  url = 'https://www.rbi.org.in/Scripts/bs_viewcontent.aspx?Id=2009/'
  u = urlopen(url)
  try:
      html = u.read().decode('utf-8')
  finally:
      u.close()
  
  soup = BeautifulSoup(html)
  for link in soup.select('div[webpartid] a'):
      href = link.get('href')
      if href.startswith('javascript:'):
          continue
      filename = href.rsplit('/', 1)[-1]
      href = urljoin(url, quote(href))
      try:
          urlretrieve(href, filename)
      except:
          print('failed to download')
  </code></pre>
  
  <p>However, when run this code does not extract the files from the target page, nor output any failure message (e.g. 'failed to download'). </p>
  
  <ul>
  <li>How can I use BeautifulSoup to select the Excel files from the page?</li>
  <li>How can I download these files to a local file using Python?</li>
  </ul>