python challenge 4解题总结


 python challenge 4网址:www.pythonchallenge.com/pc/def/linkedlist.php

打开这一关的页面只显示一个图片,按照惯例,先查看页面源码,会得出如下信息:
urllib may help. DON'T TRY ALL NOTHINGS, since it will never end. 400 times is more than enough
并且会得到一个网址linkedlist.php?nothing=12345,打开http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345后会得到如下信息:
and the next nothing is 44827
接着把nothing=12345换成nothing=44827,会得出如下信息:
and the next nothing is 45439
看到这里,并且联想提示:urllib may help. DON'T TRY ALL NOTHINGS, since it will never end. 400 times is more than enough,直接读取网页信息,利用正则表达式取出网页信息中的数字,填入nothing=*****中,直到得出结果。
(这里需要注意的是中间有部分网页会出现Yes. Divide by two and keep going.这样的信息,所以需要程序里面进行判断。)


源代码如下:

import re   
import urllib   
url="http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="  
nothing = "12345"  
search = re.compile(" (d*)$")   
search_html = re.compile(".html$")   
  
for i in xrange(300):    
    print "%s: " % nothing,    
    line = urllib.urlopen( "%s%s" % (url,nothing) ).read()   
    print line     
    if search_html.findall (line):   
        break             
    match = search.findall (line)   
    if match:   
        nothing = match [0]   
    else:   
        nothing = str (int (nothing) / 2 )  


得出的结果是:peak.html,改变url进入下一关。