异度部落格

学习是一种生活态度。

0%

Python Challenge攻略之Level 4

攻略:

首先根据提示从 linkedlist.html 转到 linkedlist.php,进入 level4
点击图片得到链接:http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345
网页内容为:and the next nothing is 44827
将 44827 替换 12345 得到网页链接:http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=44827
得到网页内容:and the next nothing is 45439
由此得知本关目的就在于利用程序循环重复以上过程直至得到结果。
中间可能会中断,可以根据提示修正。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env/python
#coding:utf8
'''
Created on May 2, 2013

@author: killua
@url: http://www.pythonchallenge.com/pc/def/equality.html
@target: http://www.pythonchallenge.com/pc/def/linkedlist.html
'''

import urllib
import re

def next_page(nothing_no):
try:
text = urllib.urlopen('http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=%s' % nothing_no).read()
print text
m = re.match(r'[^\d]*(\d+)$', text)
return m.groups()[0]
except:
return 'END'

if __name__ == '__main__':

nothing_no = 12345
#nothing_no = 16044 /2
#nothing_no = 63579
while True:
nothing_no = next_page(nothing_no)
if nothing_no.isdigit():
continue
else:
break

完整代码:

Github:https://github.com/zhenlohuang/python-challenge