1. 程式人生 > >Python 爬取網頁HTML程式碼

Python 爬取網頁HTML程式碼

#/usr/bin/env python
#-*- coding:utf-8 -*-

import urllib2
import sys
import chardet

req = urllib2.Request("http://tycool.top/")
content = urllib2.urlopen(req).read()
typeEncode = sys.getfilesystemencoding()##系統預設編碼
infoencode = chardet.detect(content).get('encoding','utf-8')##通過第3方模組來自動提取網頁的編碼
html = content.decode(infoencode,'ignore'
).encode(typeEncode)##先轉換成unicode編碼,然後轉換系統編碼輸出 print html