|
|

楼主 |
发表于 2018-2-21 23:17:07
|
显示全部楼层
import requests
from gzip import GzipFile
from io import StringIO
import zlib
def loadData(url):
headers = {'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
html = requests.get(url,headers = headers)
encoding = html.info().get('Content-Encoding')
if encoding == 'gzip':
content = gzip(html.text)
elif encoding == 'deflate':
content = deflate(html.text)
return content
def gzip(data):
buf = StringIO(data)
f = gzip.GzipFile(fileobj=buf)
return f.read()
def deflate(data):
try:
return zlib.decompress(data,-zlib.MAX_WBITS)
except zlib.error:
return zlib.decompress(data)
def main():
url = "http://tjj.zj.gov.cn/tjsj/ydsj/gy/2017/201702/t20170224_192095.html"
content = loadData(url)
with open('工业.txt','w',encoding = 'utf-8')as f:
f.write(content.text)
if __name__ == '__main__':
main()
|
|