Error downloading url file

484 views Asked by At

I get this error when I attempt to download a file from a link I get this error no matter what I try and look up and to do Im using

    urllib.error.HTTPError: HTTP Error 403: Forbidden

My code is

 elif message.content.startswith('``'):
    countn = (count+1)
    print(countn)
    print('ADD')
    meme = (message.content)
    memen = meme.replace("``", "")
    print(memen)
    print('Converted')
    urllib.request.urlretrieve(memen, meme)
    await client.send_message (message.channel, "Added!")
1

There are 1 answers

0
Sam Rockett On BEST ANSWER

403: FORBIDDEN means that the server is denying you access to the resources, either because you have failed to supply sufficient authentication or because it blocks the default python user agent (Python.urllib/3.X).

urlretrieve is actually a legacy interface as it was ported from Python 2. You should consider using urlopen instead.

Anyway, one solution to this is to add a (spoofed) user-agent to your request, which can't be done using urlretrieve...

headers = {"User-Agent": "Mozilla/5.0"}
request = urllib.request.Request(url, headers=headers)
resp = urllib.request.urlopen(request)
with open(filename, "w") as file:
    file.write(resp.read())

This wont fix issues for sites that require a login, but it will fix a lot of sites that don't.