1.urllib方法
import urllib.request#导入模块 res = urllib.request.urlopen('http://www.sina.com.cn/')#打开网页 html = res.read()#读取内容,二进制形式 html = html.decode('utf-8')#转换为utf-8 print(html)#输出
>>>type(html)
>>> str
2.requests方法
import requests#导入模块 res = requests.get('http://www.sina.com.cn/')#获得请求 res.encoding = 'utf-8'#设置编码格式 print(res.text)#以文本形式输出
>>>type(res.text)
>>>str
>>>type(res)
>>>requests.models.Response