现在的位置: 首页 > 综合 > 正文

python抓取时gzip解压

2013年03月17日 ⁄ 综合 ⁄ 共 256字 ⁄ 字号 评论关闭
'''
根据URL返回内容,有些页面可能需要gzip解压缩
'''
def getUrlContent(url):
    #返回页面内容
    doc = urllib.request.urlopen(url).read()
    #解码
    try:
        html=gzip.decompress(doc).decode("utf-8")
    except:
        html=doc.decode("utf-8")
    return html

转自:http://www.cnblogs.com/GarfieldTom/archive/2012/12/04/2802000.html

抱歉!评论已关闭.