Accessing password protected url from python script

2024/5/20 16:47:32

In python, I want to send a request to a url which will return some information to me. The problem is if I try to access the url from the browser, a popup box appears and asks for a username and password. I have a username and password for this url however, I don't know how to make python automatically complete these fields to access the URL.

This is my code that returns a 401 error:

bing = "https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/Web?Query=%
(term)s&$top=50&$format=json" %__function()
results = urllib2.urlopen(bing).read()

EDIT: Just for clarification, the function function returns a variable called term which is the term being queried in the URL

Answer

You're most likely facing HTTP basic authentication. This can be done in urllib2 but it's a bit tiresome. The instructions on how to do this are here:

http://www.voidspace.org.uk/python/articles/urllib2.shtml#id5

But if you haven't already done much work with urllib2, now is a good time to switch to the requests library. It is much more user friendly - and to do what you ask is one line call - unlike the messy way in urllib2.

requests.get('https://api.github.com/user', auth=('user', 'pass'))

More details on how requests library can be found at: http://docs.python-requests.org/en/latest/user/quickstart/#basic-authentication

To install requests library you only need to do:

sudo pip install requests

If you're on windows you should drop the sudo part.

https://en.xdnf.cn/q/73272.html

Related Q&A

Solving multiple linear sparse matrix equations: numpy.linalg.solve vs. scipy.sparse.linalg.spsolve

I have to solve a large amount of linear matrix equations of the type "Ax=B" for x where A is a sparse matrix with mainly the main diagonal populated and B is a vector. My first approach was …

I want to return html in a flask route [duplicate]

This question already has answers here:Python Flask Render Text from Variable like render_template(4 answers)Closed 6 years ago.Instead of using send_static_file, I want to use something like html(<…

Why doesnt cv2 dilate actually affect my image?

So, Im generating a binary (well, really gray scale, 8bit, used as binary) image with python and opencv2, writing a small number of polygons to the image, and then dilating the image using a kernel. Ho…

How to plot text clusters?

I have started to learn clustering with Python and sklearn library. I have wrote a simple code for clustering text data. My goal is to find groups / clusters of similar sentences. I have tried to plot…

Selenium - Unresponsive Script Error (Firefox)

This question has been asked before, but the answer given does not seem to work for me. The problem is, when opening a page using Selenium, I get numerous "Unresponsive Script" pop ups, refe…

Fail to validate URL in Facebook webhook subscription with python flask on the back end and ssl

Im trying to start using new messenger platform from FB. So i have server with name (i.e.) www.mysite.com I got a valid SSL certificate for that domain and apache is setup correctly - all good.I have …

What is a proper way to test SQLAlchemy code that throw IntegrityError?

I have read this Q&A, and already try to catch exception on my code that raise an IntegrityError exception, this way :self.assertRaises(IntegrityError, db.session.commit())But somehow my unit test …

What are the different options for social authentication on Appengine - how do they compare?

[This question is intended as a means to both capture my findings and sanity check them - Ill put up my answer toute suite and see what other answers and comments appear.]I spent a little time trying t…

Is there any way to get source code inside context manager as string?

Source code of function can be received with inspect.getsourcelines(func) function. Is there any way to do same for context manager?with test():print(123)# How to get "print(123)" as line he…

Create temporary file in Python that will be deleted automatically after sometime

Is it possible to create temporary files in python that will be deleted after some time? I checked tempfile library which generates temporary files and directories.tempfile.TemporaryFile : This functi…