python challenge - orc.py

http://www.pythonchallenge.com/pc/def/ocr.htmlhtml

    recognize the characters. maybe they are in the book, 
but MAYBE they are in the page source.python

打開頁面源代碼,能夠看到下面的信息:正則表達式

<!--
find rare characters in the mess below:
-->

  經過給出的提示「find rare characters in the mess below」,咱們能夠知道線索就在第二個<!-- -->中,不失通常性,設計python代碼以下:url

import re   
import urllib
import string

# 使用urllib模塊讀取頁面源代碼     
sock = urllib.urlopen("http://www.pythonchallenge.com/pc/def/ocr.html")   
source = sock.read()   
sock.close()   

# 標誌re.S表示在正則表達式中點(.)能夠匹配任意字符,包括換行符
data = re.findall(r'<!--(.+?)-->', source, re.S)
charList = re.findall(r'([a-zA-Z])', data[1], 16)

# 使用string模塊將list轉爲字符串打印
print string.join(charList)

輸出:spa

「e q u a l i t y」設計

下一關:http://www.pythonchallenge.com/pc/def/equality.htmlcode

urllib模塊

相關文章
相關標籤/搜索