Python抓需要输入用户名密码的https网页

import urllib2
import locale

import sys

__g_codeset = sys.getdefaultencoding()
if "ascii"==__g_codeset:
    __g_codeset = locale.getdefaultlocale()[1]

def utf8_to_mbs(s):
    return s.decode("utf-8").encode(__g_codeset)
#
def mbs_to_utf8(s):
    return s.decode(__g_codeset).encode("utf-8")
#
def _queryRows(cu,sql):
    try:
        cu.execute(sql)
        rows = []
        for row in cu:
            rows.append(row)
        #
        return rows
    except:
        print(traceback.format_exc())
    #end
#

#http
response = urllib2.urlopen('http://python.org/')
html = response.read()
print html
#https
url = "https://www.xxx.com/xxx/xxx"
# Add username and password.
username = "scott"
password = "tiger"
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, url, username, password)
handler = urllib2.HTTPDigestAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
# use the opener to fetch a URL
urllib2.install_opener(opener)
response = urllib2.urlopen(url)
file = open("a.html", "w")
file.write(response.read())
file.close()

你可能感兴趣的:(C++/Python/java)