Python Challenge 4
Challenge 4
Challenge 4的图片上有一个链接http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345,打开后提示,下一个nothing是44827。一定是把URL中的nothing改为nothing=44827
。之后,这个nothing返回的下一个值是45439,显然这个过程不能够完全通过手动完成,万一它有千八百个递归呢?那么就使用Python去自动获取结果吧。
回到最初的页面,页面源码中的注释说:
urllib may help. DON’T TRY ALL NOTHINGS, since it will never end. 400 times is more than enough.
使用urllib中的urlopen方法即可获取返回的内容了,为了防止作者故意打乱格式,对找到的nothing值要进行筛选,并且将400作为遍历上限:
import urllib,re
def findurl(start):
base_url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="
rule = r'\d+'
url = base_url + start
for x in range(400):
f = urllib.urlopen(url)
content = f.read()
tail = re.findall(rule, content)
print tail
if len(tail) == 1:
url = base_url + tail[0]
elif len(tail) > 1:
print "there are more than one nothing here.\nplease check out and go on.\n"
return "%s\nContents:\n%s" % (url, content)
else:
return "%s\nContents:\n%s" % (url, content)
s1 = "12345"
print findurl(s1)
函数执行到http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=16044时停止,返回一句话:
Yes. Divide by two and keep going.
果然有诈,继续!
print findurl("8022")
执行到http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=82682时返回了两个值,内容为:
There maybe misleading numbers in the text. One example is 82683. Look only for the next nothing and the next nothing is 63579
真狡猾,Keep going:
print findurl("63579")
终于在http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=66831找到了答案:peak.html
打开URL:http://www.pythonchallenge.com/pc/def/peak.html,进入Challenge 5。
博客中 Python Challenge 所有源码分享:打开坚果