import chardet #字符集检测 import urllib.request # 网址 url = "http://www.baidu.com/"
def automatic_detect(url): content=urllib.request.urlopen(url).read() result=chardet.detect(content) encoding=result['encoding'] return encoding
print(url) print(automatic_detect(url))
# 请求 request = urllib.request.Request(url) # 爬取结果 response = urllib.request.urlopen(request) data = response.read() # 设置解码方式 data = data.decode('utf-8')
# 打印爬取网页的各类信息 # 打印结果 print(data) print(type(response)) print(response.geturl()) print(response.info()) print(response.getcode())
需要安装chardet
(adsbygoogle = window.adsbygoogle || []).push({});