The Python Challenge(7)

问题链接

问题链接如下:

http://www.pythonchallenge.com/pc/def/channel.html

答案链接

答案链接如下:

http://www.pythonchallenge.com/pc/def/oxygen.html

解题思路

根据拉链,得到第一个关键字zip,再继续看源码,有如下信息:


试试将URL改为:http://www.pythonchallenge.com/pc/def/channel.zip,下载解压后,查看其中的readme.txt,有如下信息:

welcome to my zipped list.

hint1: start from 90052
hint2: answer is inside the zip

就是说,答案在zip本身,则有如下代码:

from os import path
from urllib import request
from zipfile import ZipFile
import re

channel_zip = path.join('/tmp', "channel.zip")

url = 'http://www.pythonchallenge.com/pc/def/channel.zip'
response = request.urlopen(url)
content = response.read()
with open(channel_zip, 'wb') as channel:
    channel.write(content)

channel = ZipFile(channel_zip, 'r')
hint = 'readme.txt'

while True:
    txt = str(channel.read(hint), 'utf-8')
    
    # Step 1
    print(txt)

    ret = re.findall('\W(\d+)(?:\s|$)', txt)
    if len(ret) == 0:
        break
    hint = ret[0]+'.txt'

最终有如下信息:

Collect the comments.

接下来修改代码,阅读zip的附件信息,则有如下代码:

from os import path
from urllib import request
from zipfile import ZipFile
import re

channel_zip = path.join('/tmp', "channel.zip")

url = 'http://www.pythonchallenge.com/pc/def/channel.zip'
response = request.urlopen(url)
content = response.read()
with open(channel_zip, 'wb') as channel:
    channel.write(content)

channel = ZipFile(channel_zip, 'r')
hint = 'readme.txt'
comment = ''

while True:
    txt = str(channel.read(hint), 'utf-8')
    
    # Step 1
    # print(txt)
    
    # Step 2
    print(str(channel.getinfo(hint).comment, 'utf-8'), end='')

    ret = re.findall('\W(\d+)(?:\s|$)', txt)
    if len(ret) == 0:
        break
    hint = ret[0]+'.txt'

输出信息如下所示:

****************************************************************
****************************************************************
**                                                            **
**   OO    OO    XX      YYYY    GG    GG  EEEEEE NN      NN  **
**   OO    OO  XXXXXX   YYYYYY   GG   GG   EEEEEE  NN    NN   **
**   OO    OO XXX  XXX YYY   YY  GG GG     EE       NN  NN    **
**   OOOOOOOO XX    XX YY        GGG       EEEEE     NNNN     **
**   OOOOOOOO XX    XX YY        GGG       EEEEE      NN      **
**   OO    OO XXX  XXX YYY   YY  GG GG     EE         NN      **
**   OO    OO  XXXXXX   YYYYYY   GG   GG   EEEEEE     NN      **
**   OO    OO    XX      YYYY    GG    GG  EEEEEE     NN      **
**                                                            **
****************************************************************
 **************************************************************

对应于字符串hockey,将URL改为: http://www.pythonchallenge.com/pc/def/hockey.html,输入到浏览器中,最终页面显示:

it's in the air. look at the letters.

再次观察组成hockey的字符,则得到字符串oxygen,则最终URL为: http://www.pythonchallenge.com/pc/def/oxygen.html

你可能感兴趣的:(The Python Challenge(7))