python Python模块:urllib:request,urlopen和gzip

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Python模块:urllib:request,urlopen和gzip相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python3
'''
Request debian.org homepage. Add header Accept-Encoding:gzip to request
compressed file. Decompress file. Print using for loop while decoding
to utf-8.
'''

from urllib.request import Request, urlopen
import gzip

req = Request('http://www.debian.org')
req.add_header('Accept-Encoding', 'gzip')
response = urlopen(req)
content = gzip.decompress(response.read())
decomp_req = content.splitlines()
for line in decomp_req:
    print(line.decode('utf-8'))

以上是关于python Python模块:urllib:request,urlopen和gzip的主要内容,如果未能解决你的问题,请参考以下文章

python3.5中没有urllib2怎么处理?

python2.7导入mysqldb

python—爬虫

python—爬虫

python爬虫思路

python爬虫思路