urllib.request.urlopen(url) with Authentication

前端 未结 3 1226
庸人自扰
庸人自扰 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 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.

提交回复
热议问题