地址:http://www.pythonchallenge.com/pc/def/channel.html
代码
from urllib import request
import io
import re
import zipfile
file_name_next = "90052"
comment = ''
zip_get = request.urlopen("http://www.pythonchallenge.com/pc/def/channel.zip")
zip_file = io.BytesIO(zip_get.read())
channel_zip = zipfile.ZipFile(zip_file, "r")
while True:
file_comment = channel_zip.getinfo(file_name_next+".txt").comment.decode("utf-8")
comment += file_comment
with channel_zip.open(file_name_next+".txt", "r") as f:
file_content = f.read().decode('utf-8')
try:
file_name_next = re.search(r"\d+", file_content).group(0)
except:
break
print(comment)
zip_file.close()
运行结果
学到的东西
import io
StringIO和BytesIO是在内存中操作str和bytes的方法,使得和读写文件具有一致的接口。
s = StringIO()
s.write(‘hello’)
s.write(’ ‘)
s.write(‘world’)
s.getvalue()
‘hello world’