[Python Challenge通关]第6关 now there are pairs

[Python Challenge通关]第6关 now there are pairs_第1张图片
channel

挑战地址,点我

分析

右键查看网页源代码看一下:

 

  now there are pairs
  



中间一长段的注释是在说捐赠的事情,并且作者说明了与这一关的问题没有关系。

那唯一的提示,就是最上面的 `` zip 常见的是压缩包的意思,其实还有拉链的意思,这就和图片对应起来了。

再看下这一关的主题now there are pairs,关键词 pairs 成对的,zip 左侧还有一个箭头指向 html

联想一下这些线索,猜想把 url 中的 html 替换为 zip http://www.pythonchallenge.com/pc/def/channel.zip。

确实如此,我们下载 channel.zip 压缩包,解压看一下。

里面有 910 个数字命名的文本文件,其中有一个 readme.txt,打开看一下:

welcome to my zipped list.

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

根据提示,去找下名字为 90052 的文件:

Next nothing is 94191

这和第 4 关类似,也是一个链表结构,用代码实现看下,这里用到了 zipfile 包来解压 zip 文件。

#!/usr/bin/env/ python3

import zipfile
import re

infile = "channel.zip 文件路径"

# 使用 zipfile 包解压并读取文件内容到 files
files = {}
with zipfile.ZipFile(infile) as fzip:
    for name in fzip.namelist():
        with fzip.open(name) as f:
            files[name] = f.read().decode("utf-8")

# readme.txt 中 nothing 初始值
nothing = "90052"

while True:
    f = nothing + ".txt"
    if f in files:
        print(files[f])
        result = re.search(r"Next nothing is (\d+)", files[f])
        try:
            nothing = result.group(1)
        except:
            break

输出结果:

Next nothing is 67824
Next nothing is 46145
Collect the comments.

提示信息 Collect the comments.,网页源码并没有什么有意义的 comments,也许 comments 也在压缩包里面?

查看下 zipfile 包的文档说明,有个 getinfo 方法,

调用该方法会返回一个 ZipInfo Objects,这个对象确实有个 comment 属性。

和上面的联系起来,在遍历整个链表的时候,获取每个文件的 comments 看下:

#!/usr/bin/env/ python3

import zipfile
import re

infile = "/home/roger/Documents/Resource/channel.zip"

# 使用 zipfile 包解压并读取文件内容到 files
files = {}
fzip = zipfile.ZipFile(infile)
for name in fzip.namelist():
    with fzip.open(name) as f:
        files[name] = f.read().decode("utf-8")

# readme.txt 中 nothing 初始值
nothing = "90052"

while True:
    f = nothing + ".txt"
    # 获取 comment 并输出结果
    print(fzip.getinfo(f).comment.decode("utf-8"), end="")
    if f in files:
        # print(files[f])
        result = re.search(r"Next nothing is (\d+)", files[f])
        try:
            nothing = result.group(1)
        except:
            break

输出内容:

****************************************************************
****************************************************************
**                                                            **
**   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      **
**                                                            **
****************************************************************
 **************************************************************

comment 中的字符组成了一个单词 hockey,用它来替换当前页面的 url 得到 http://www.pythonchallenge.com/pc/def/hockey.html

打开看到如下内容:

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

翻译一下:它在空气中。 看看这些字母。,回头看下那些字符,hockey 是由 O X Y G E N 组成的,连起来就是 oxygen,这也是一个单词,氧气的意思,和提示内容符合。

oxygen 替换 url 得到下一关真正的入口了 http://www.pythonchallenge.com/pc/def/oxygen.html

参考资源:

  1. zipfile 官方文档

你可能感兴趣的:([Python Challenge通关]第6关 now there are pairs)