The Python Challenge Level-2 Solution
先附上我在Github上存放的代碼倉庫: The Python Challenge
The Python Challenge第二關給了我們一張書的圖片,但是十分模糊的,什麼都看不清,這個時候,我們看看它給我們的文字提示是
MAYBE they are in the page source.
這個時候我們點擊鼠標右鍵,查看網頁的源代碼,會發現,果然有一大長條亂七八糟的符號。不難猜到,我們所需要的下一關的url就藏在這堆符號裏。於是我們用簡單的爬蟲爬取網頁代碼,然後利用正則表達式來捕捉我們需要的字母。
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'Yuuki_Dach'
from urllib import request
import re
pyUrl = "http://www.pythonchallenge.com/pc/def/ocr.html"
req = request.Request(pyUrl)
resq = request.urlopen(req)
content = resq.read()
book = re.compile(b"<!--(.*?)-->", re.S)
bookContents = re.findall(book, content)
wordsCmpl = re.compile(b"([ a-zA-Z])", re.S)
for bookContent in bookContents:
words = re.findall(wordsCmpl, bookContent)
code = ""
for word in words:
code += word.decode("ascii")
print(code)
這裏只要注意一下編碼的方式就好。
運行代碼,我們可以得到如下信息
find rare characters in the mess below
equality
將equality輸入到url就能進入下一關了