pythonpython-requestshyperlinkget-request

How to get data from requests in python with a downloading link


I want to get a file in my project using the requests module of python but the link that leads to that file starts downloading the file as it is clicked.

Like this one https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data

If I send a get request, it responds with the status

data = requests.get("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data")
print(data)

For this code the output is

<Response [200]>

But I want the file which is downloaded with link is clicked.

Is it possible to do so

Thanks in advance


Solution

  • data is raw, it includes response header and body, what you want is data.content (binary), there are also data.text for string and data.json() for json object

    data = requests.get("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data")
    # save to local storage
    with open('iris.data', 'wb') as f:
        f.write(data.content)