Python Challenge 第四题

这一题没有显示提示语,仅仅有一幅图片,图片也看不出什么名堂,于是直接查看源代码,源代码例如以下:
<html>
<head>
  <title>follow the chain</title>
  <link rel="stylesheet" type="text/css" href="../style.css">
</head>
<body>
<!-- urllib may help. DON'T TRY ALL NOTHINGS, since it will never 
end. 400 times is more than enough. -->
<center>
<a href="linkedlist.php?nothing=12345"><img src="chainsaw.jpg" border="0"/></a>
<br><br><font color="gold"></center>
Solutions to previous levels: <a href="http://wiki.pythonchallenge.com/"/>Python Challenge wiki</a>.
<br><br>
IRC: irc.freenode.net #pythonchallenge
</body>
</html>

能够看到,解题须要用到urllib,提示我们不要去一个一个地尝试noting,由于这数量特别多,远远不止四百次(终于证明事实上不须要四百次,一百二十多次即可了),既然这么多,那肯定不能手动去试了,可是能够让计算机自己主动去訪问提取nothing值。点击图片,我们便获得了第一个nothing值44827,再改动URL,获得下一个nothing值,发现没个nothing页面最后都会有这句话“and the next nothing is ”后面接着一个数字(当然,由于URL的须要,我们把它理解为一个字符串,刚開始投机取巧觉得这数字是五位的,后来出现四位的,又觉得是四位或五位的,再后来有三位的。。。于是还是老老实实写关于提取最后不限位数数字字符的代码),从而猜想,那个能够获得答案相关信息的页面内容会与这些均不同,能够通过内容最后是否是数字字符来推断,最后代码例如以下:

import urllib.request
name='http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
finalNum='44827'#nothing's number string
while 1:
    f=urllib.request.urlopen(name+x)
    content=f.read().decode('utf-8')
    print(content)        #输出页面内容,方便查看到最后的解题信息
    finalNum=[]
    content=list(content)
    content.reverse()
    for i in content:     #提出页面内容最后的数字字符
        if i.isdigit():
            finalNum.append(i)
        else:
            break
    finalNum.reverse()
    finalNum=''.join(finalNum)
    if not finalNum:      #推断页面最后内容是不是数字字符,假设不是,则跳出循环
        break

当循环结束时候的那个页面信息是“Yes. Divide by two and keep going”,上一个页面为“and the next nothing is 16044”,于是改动finalNum的值为‘8022’,又一次启动程序,最后获得答案

原文地址:https://www.cnblogs.com/zfyouxi/p/4184744.html