user3063129
user3063129

Reputation: 13

HTTP Error 401: Authorization Required while downloading a file from HTTPS website and saving it

Basically i need a program that given a URL, it downloads a file and saves it. I know this should be easy but there are a couple of drawbacks here...

First, it is part of a tool I'm building at work, I have everything else besides that and the URL is HTTPS, the URL is of those you would paste in your browser and you'd get a pop up saying if you want to open or save the file (.txt).

Second, I'm a beginner at this, so if there's info I'm not providing please ask me. :)

I'm using Python 3.3 by the way.

I tried this:

import urllib.request
response = urllib.request.urlopen('https://websitewithfile.com')
txt = response.read()
print(txt)

And I get:

urllib.error.HTTPError: HTTP Error 401: Authorization Required

Any ideas? Thanks!!

Upvotes: 1

Views: 15875

Answers (4)

imankalyan
imankalyan

Reputation: 195

You can try this solution: https://github.qualcomm.com/graphics-infra/urllib-siteminder

import siteminder
import getpass
url = 'https://XYZ.dns.com'
r = siteminder.urlopen(url, getpass.getuser(), getpass.getpass(), "dns.com")
Password:<Enter Your Password>

data = r.read() / pd.read_html(r.read())  # need to import panda as pd for the second one

Upvotes: 0

Bin TAN - Victor
Bin TAN - Victor

Reputation: 362

If you don't have Requests module, then the code below works for python 2.6 or later. Not sure about 3.x

import urllib

testfile = urllib.URLopener()
testfile.retrieve("https://randomsite.com/file.gz", "/local/path/to/download/file")

Upvotes: 2

Back2Basics
Back2Basics

Reputation: 7806

You can do this easily with the requests library.

import requests
response = requests.get('https://websitewithfile.com/text.txt',verify=False, auth=('user', 'pass'))
print(response.text)

to save the file you would type

with open('filename.txt','w') as fout:
   fout.write(response.text):

(I would suggest you always set verify=True in the resquests.get() command)

Here is the documentation:

Upvotes: 6

ajm475du
ajm475du

Reputation: 371

Doesn't the browser also ask you to sign in? Then you need to repeat the request with the added authentication like this:

Upvotes: 2

Related Questions