urllib.request.urlopen(url) with Authentication

前端 未结 3 1213
庸人自扰
庸人自扰 2020-12-05 14:12

I\'ve been playing with beautiful soup and parsing web pages for a few days. I have been using a line of code which has been my saviour in all the scripts that I write. The

相关标签:
3条回答
  • 2020-12-05 14:48

    Have a look at the HOWTO Fetch Internet Resources Using The urllib Package from the official docs:

    # create a password manager
    password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
    
    # Add the username and password.
    # If we knew the realm, we could use it instead of None.
    top_level_url = "http://example.com/foo/"
    password_mgr.add_password(None, top_level_url, username, password)
    
    handler = urllib.request.HTTPBasicAuthHandler(password_mgr)
    
    # create "opener" (OpenerDirector instance)
    opener = urllib.request.build_opener(handler)
    
    # use the opener to fetch a URL
    opener.open(a_url)
    
    # Install the opener.
    # Now all calls to urllib.request.urlopen use our opener.
    urllib.request.install_opener(opener)
    
    0 讨论(0)
  • 2020-12-05 14:48

    With urllib3 :

    import urllib3
    
    http = urllib3.PoolManager()
    myHeaders = urllib3.util.make_headers(basic_auth='my_username:my_password')
    http.request('GET', 'http://example.org', headers=myHeaders)
    
    0 讨论(0)
  • 2020-12-05 15:07

    You're using HTTP Basic Authentication:

    import urllib2, base64
    
    request = urllib2.Request(url)
    base64string = base64.b64encode('%s:%s' % (username, password))
    request.add_header("Authorization", "Basic %s" % base64string)   
    result = urllib2.urlopen(request)
    

    So you should base64 encode the username and password and send it as an Authorization header.

    0 讨论(0)
提交回复
热议问题