Maysam
Maysam

Reputation: 13

Unable to download files using Python Requests

Files can downloaded manually from HLTV (enter url & Enter, files get downloaded) but not working with Python requests and returns an unknown 9KB file.

Toolset: Requests 2.25.1, Python 3.9 64-bit, PyCharm 2021.1.1 Professional Edition, Windows 10.

Below is a minimal working example:

import requests
r = requests.get('https://www.hltv.org/download/demo/64081')
with open('C:\\new\\test.rar', 'wb') as f:
    f.write(r.content)

Upvotes: 1

Views: 1073

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195408

Try to specify User-Agent HTTP header:

import requests

url = "https://www.hltv.org/download/demo/64081"

headers = {
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0"
}

with open("test.rar", "wb") as f:
    f.write(requests.get(url, headers=headers).content)

Writes test.rar (output from ls -alF test.rar):

-rw-r--r-- 1 root root 279701450 May  3 22:27 test.rar

Upvotes: 1

Related Questions