`
he_lux
  • 浏览: 102909 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

python challenge 4

阅读更多
第四题的画面上没有任何提示,直接查看源代码,有一句:
urllib may help. DON'T TRY ALL NOTHINGS, since it will never
end. 400 times is more than enough.
DON'T TRY ALL NOTHINGS是什么意思?

点击画面上的图片,跳转到http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345,画面上显示:and the next nothing is 92512
这下明白了,要修改URL中nothing后面的数字。改吧!当改到第三次的时候,画面上提示:Your hands are getting tired and the next nothing is 50010。 呵呵,有意思,印证了这句话:it will never end. 400 times is more than enough. 老老实实写程序吧。  最后的结果是peak.html
import urllib
import re

if __name__ == '__main__':
    
    f = open('4.txt', 'w')
    
    url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
    nothing = '12345'
    #'46059' is from the result of execution of '12345'
    #nothing = '46059'
    
    # solution 1 Start
    for i in range(500):
    
        response = urllib.urlopen(url + nothing)
        page = response.read()
        
        f.write(str(i) + ': ' +page + '\n')
        
        list = re.findall('the next nothing is \d*', page)
        
        if len(list) > 0:
            nothing = ''.join(d for d in list[0] if d.isdigit())
        else:
            break
    #solution 1 End
        
    #solution 2 Start:
    print('solution 2: ')
    
    findNothing = re.compile('the next nothing is (\d)*').search
    while True:
        text = urllib.urlopen(url + nothing).read()
        print(text)
        match = findNothing(text)
        if match:
            nothing = match.group(1)
        else:
            break
        
    #solution 2 End:
        
    f.close()

学习到了urllib的简单使用,javascript的group的用法。
0
0
分享到:
评论
1 楼 duolaimifa 2014-02-23  
你的意思是,执行到第500次的时候返回peak吗?

相关推荐

Global site tag (gtag.js) - Google Analytics