欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python challenge 4 博客分类: python PythonPHPJavaScriptF#HTML 

程序员文章站 2024-02-04 11:35:16
...
第四题的画面上没有任何提示,直接查看源代码,有一句:
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的用法。