SD4
SD4

Reputation: 479

HTTP 403 error when trying to download a file from URL using Python

I'm trying to download a file from the URL -> https://www.microsoft.com/en-us/download/confirmation.aspx?id=56519

I can manually download the file by accessing the URL via a browser and the file gets automatically saved onto the local machine in the Downloads folder. (The file is in JSON format)

However, I need to achieve this using a Python script. I tried using urllib.request & wget, but in both cases I keep getting the error -

    urllib.request.urlretrieve(url, path)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 247, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 531, in open
    response = meth(req, response)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 641, in http_response
    'http', request, response, code, msg, hdrs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 569, in error
    return self._call_chain(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 503, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 649, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

Upvotes: 0

Views: 1678

Answers (2)

Kevin Lu-MSFT
Kevin Lu-MSFT

Reputation: 35194

Is there a workaround to this? Dealing with dynamic changes ?

You could try the following script to get the download url and download the json file:

import requests
import re
import urllib.request

rq= requests.get("https://www.microsoft.com/en-us/download/confirmation.aspx?id=56519")
 
t = re.search("https://download.microsoft.com/download/.*?\.json", rq.text )
 


a= t.group()

print(a)

path = r"$(Build.sourcesdirectory)\agent.json"
urllib.request.urlretrieve(a, path)

Result:

enter image description here

Upvotes: 3

scalloty
scalloty

Reputation: 118

Python 3

import urllib.request, json 
with urllib.request.urlopen("https://download.microsoft.com/download/7/1/D/71D86715-5596-4529-9B13-DA13A5DE5B63/ServiceTags_Public_20210329.json") as url:
    data = json.loads(url.read().decode())
    print(data)

enter image description here

Upvotes: 1

Related Questions