Python Challenge - 4
http://www.pythonchallenge.com/pc/def/linkedlist.html
输入网址后会提示linkedlist.php 跳转到
http://www.pythonchallenge.com/pc/def/linkedlist.php
看到提示说follow the chain这一关需要抓取URL一直Follow下去
点击图片进入到http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345
提示and the next nothing is 92512 更换12345为92512后访问得到下一个数字
做一个递归调用自身的函数可以解决这个问题.
程序执行到92118时,返回:
and the next nothing is 92118 Yes. Divide by two and keep going.
需要把得到的数字除以2才能继续 使用try,except可以捕捉到这个没有抓到数字的情况单独进行处理
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import urllib,re
def read(nothing):
try:
head = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
s = urllib.urlopen(head + nothing).read()
print s
# 使用正则表达式匹配抓取到的页面中的最后一个数字
pat = re.compile('([0-9]+)')
nothing = re.findall(pat,s)[-1]
# 返回函数本身 使函数一直执行下去
return read(nothing)
except:
if 'Divide' in s:
return read(str(int(nothing)/2))
else:
pass
if __name__ == '__main__':
read('12345')
raw_input('<PRESS ENTER>')
最终得到下一关的地址:
浙公网安备 33010602011771号