import chardet #字符集檢測
import urllib.request
# 網址
url = "http://www.baidu.com/"url
def automatic_detect(url):
content=urllib.request.urlopen(url).read()
result=chardet.detect(content)
encoding=result['encoding']
return encoding code
print(url)
print(automatic_detect(url))utf-8
# 請求
request = urllib.request.Request(url)
# 爬取結果
response = urllib.request.urlopen(request)
data = response.read()
# 設置解碼方式
data = data.decode('utf-8')get
# 打印爬取網頁的各種信息
# 打印結果
print(data)
print(type(response))
print(response.geturl())
print(response.info())
print(response.getcode())import
須要安裝chardetcoding