I want to download the Excel file on this ONS webpage using the MechanicalSoup package in Python. I have read the MechanicalSoup documentation. I have searched extensively for an example to follow, on StackOverflow and elsewhere, without luck.
My attempt is:
# Install dependencies
# pip install requests
# pip install BeautifulSoup4
# pip install MechanicalSoup
# Import libraries
import mechanicalsoup
import urllib.request
import requests
from bs4 import BeautifulSoup
# Create a browser object that can collect cookies
browser = mechanicalsoup.StatefulBrowser()
browser.open("https://www.ons.gov.uk/economy/grossdomesticproductgdp/timeseries/l2kq/qna")
browser.download_link("https://www.ons.gov.uk/generator?format=xls&uri=/economy/grossdomesticproductgdp/timeseries/l2kq/qna")
In that last line, I have also tried:
browser.download_link(link="https://www.ons.gov.uk/generator?format=xls&uri=/economy/grossdomesticproductgdp/timeseries/l2kq/qna",file="c:/test/filename.xls")
Update 25 Jan 2019: And thanks to AKX's comment below, I've tried
browser.download_link(re.escape("https://www.ons.gov.uk/generator?format=xls&uri=/economy/grossdomesticproductgdp/timeseries/l2kq/qna"))
In each case, I get the error:
mechanicalsoup.utils.LinkNotFoundError
Yet the link does exist. Try pasting this into your address bar to confirm:
https://www.ons.gov.uk/generator?format=xls&uri=/economy/grossdomesticproductgdp/timeseries/l2kq/qna
What am I doing wrong?
Update 2, 25 Jan 2019: Thanks to AKX's answers below, this is the full MWE that answers my question (posting for anyone who encounters the same difficulty later):
# Install dependencies
# pip install requests
# pip install BeautifulSoup4
# pip install MechanicalSoup
# Import libraries
import mechanicalsoup
import urllib.request
import requests
from bs4 import BeautifulSoup
import re
# Create a browser object that can collect cookies
browser = mechanicalsoup.StatefulBrowser()
browser.open("https://www.ons.gov.uk/economy/grossdomesticproductgdp/timeseries/l2kq/qna")
browser.download_link(link_text=".xls",file="c:/py/ONS_Data.xls" )