import urllib.request
def openUrl(url):
req = urllib.request.Request(url)
req.add_header('User-Agent',''Mozilla/5.0 (Windows NT 6.1; \
WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'')
response = urllib.request.urlopen(req)
html = response.read().decode('raw_unicode_escape')
return html
if __name__ == '__main__':
url = 'https://www.baidu.com'
html = openUrl(url)重点在于编码
raw_unicode_escape