http://www.pythonchallenge.com/pc/def/ocr.html
看提示, 下载页面的源代码, 源代码的注释中有本关需要求解的问题
问题是在给定的字符串中找出出现的英文字符, 中间夹杂的特殊字符有
!@#$%^&*()_+{}[]
代码如下
import re
import urllib.request
page = urllib.request.urlopen("http://www.pythonchallenge.com/pc/def/ocr.html")
text_bytes = page.read()
page.close()
text = text_bytes.decode("utf8")
find_begin = text.find('-->') + len('-->')
text = text[find_begin:]
result = re.findall(r'[a-z]', text)
print(''.join(result))
注意Python3和Python2的区别, Python3需要import urllib.request而Python2则import urllib
import re为正则表达式匹配模块
Python3使用page.read()后得到的是字节包, 需要进行解码才能变成字符串 (代码第7行)
得到结果为:
equality
将其输入url可以进入下一关