星期三, 7月 15, 2009

Python Challenge - Level 2

Level 2

圖下給的提示為
recognize the characters. maybe they are in the book,
but MAYBE they are in the page source

於是直接打開page source看看網頁原始碼,可以看到有註解部份,寫著:


&1t;!--
find rare characters in the mess below:
--&1t;

&1t;!--
%%$@_$^__#)^)&!_+]!*@&^}@[@%]()%+$&[(_@%+%$*^@$^!+]!&_#)_*}{}}!}_]$[%}@[{_@#_^{*
+_*{@+[$!!@%$+{_&(#^(([&[][[&@#+}_]&&]}^*&$&)#_^$@$((%)}+{}$#+{+^}&[#[#_+${#[#]{
(@@[%}[}$%+*#$+[%(**!$+@$@&+$_$#!_&&&&{***+)}][}#^!%#&$*)$!%}*&#}}##(^_%^]{+]&&]
}^]#^(}@]&$]*_][])$]{_+})^_}]))......
--&1t;


於是就需要找出一堆奇怪字元中的字母,用urllib2打開後,再用HTMLParser裏面的handle_comment來處理,就OK了!


#!/usr/bin/env python

from HTMLParser import HTMLParser
import urllib2

class Parser(HTMLParser):
def handle_comment(self, data):
s = ""
for x in data:
if x.isalpha():
s = s + x
print s

page = urllib2.urlopen("http://www.pythonchallenge.com/pc/def/ocr.html")
p = Parser()
p.feed(page.read())


跑出來的output:

cacaegg@cacabook:~/workspace/Python Challenge/src$ ./level_2.py
findrarecharactersinthemessbelow
equality


Next :D

Level 3

沒有留言: