pythondata-sciencedoi

How to download XLSX file from DOI link?


I want to download two files automatically from Python for a reproducible statistical analysis.

These links

I tried

import requests

url = 'https://doi.org/10.1371/journal.pone.0282068.s001'

response = requests.get(url)

I suspect that the file is actually the content of response.content, which appears to be a bunch of encoded information (e.g. \xe2\x81a\xe4\x1dq\xbe9~3\x94\x885\xba\xc8\x9bz\'~\x1c)X>\xaaXyg\x929\xf84\xc2\x06\t\n x5\).

How do I download these files and save them as XLSX files?


Solution

  • you need to save the content into the file.

    import requests
    
    url = 'https://doi.org/10.1371/journal.pone.0282068.s001'
    
    response = requests.get(url)
    data = response.content
    
    with open('filename.xlsx', 'wb') as f:
        f.write(data)
    

    your content will be saved in file filename.xlsx