---
title: "python challenge 4"
author: "he_lux"
date: "2009-12-28T18:16:01Z"
tags: ["F#", "HTML", "JavaScript", "PHP", "Python"]
categories: ["编程语言"]
slug: "python-challenge-4"
draft: false
lastmod: "2009-12-28T18:17:37Z"
id: 558501
url: "https://hhlux.iteye.com/blog/558501"
---

第四题的画面上没有任何提示，直接查看源代码，有一句：
urllib may help. DON'T TRY ALL NOTHINGS, since it will never 
end. 400 times is more than enough.
<span style="color:red">DON'T TRY ALL NOTHINGS</span>是什么意思？

点击画面上的图片，跳转到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

```python

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的用法。