Level 3:
http://www.pythonchallenge.com/pc/def/equality.html
根据hints,打开page source code,查找前后各仅有3个大写字母包围的小写字母。
#!/usr/bin/python import urllib.request import re """ Python Challenge, Level 3: http://www.pythonchallenge.com/pc/def/equality.html """ def main(): url = 'http://www.pythonchallenge.com/pc/def/equality.html' resp = urllib.request.urlopen(url).read().decode() match = re.search(r'<!--(.*?)-->', resp, re.DOTALL) result = ''.join(re.findall(r'[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]', match.group(1))) print(result) if __name__ == '__main__': main()
得到结果linkedlist,还是老办法,替换url,进入下一关。
Level 4:
http://www.pythonchallenge.com/pc/def/linkedlist.php
图没懂,其实就是个链接,具体打开page source code可以看到。链接为一个php的页面,带有参数nothing,访问可以得到信息“and the next nothing is 44827”,参数值迭代更新;编码追踪,直至找到第一个非此类信息。
#!/usr/bin/python import urllib.request import re """ Python Challenge, Level 4: http://www.pythonchallenge.com/pc/def/linkedlist.php """ def pingurl(common_url, initial): while(initial): url = common_url + initial resp = urllib.request.urlopen(url).read().decode() match = re.search(r'and the next nothing is (\d+)', resp) if match : initial = match.group(1) print(initial) else : return resp def main(): url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=' initial = '12345' print(pingurl(url, initial)) if __name__ == '__main__': main()
初始值12345,若干次访问后得到信息“Yes. Divide by two and keep going.”;对最后一个值16044整除2,作为新的初始值继续,最终得到peak.html。替换url,进入下一关:)