有时候我们需要python解析一些页面,实现自动化监控的功能。而这些页面一般需要用户输入username,password进行基本的验证,这时就需要我们使用python基本的认证功能。现已python抓取dubbo-admin的服务页面进行举例:
# create a password manager
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
# Add the username and password.
top_level_url = "http://1000.906.400.1620:88889/dubbo-admin/governance/services"
username = "root"
password = "testroot"
password_mgr.add_password(None, top_level_url, username, password)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
# create "opener" (OpenerDirector instance)
opener = urllib2.build_opener(handler)
# use the opener to fetch a url
opener.open(top_level_url)
# Install the opener
urllib2.install_opener(opener)
# Now all calls to use urllib2.urlopen use our opener.
print urllib2.urlopen(top_level_url).read()