您的位置:首页 > Web前端 > HTML

Python 爬取网页HTML代码

2016-09-29 19:43 323 查看
#/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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: