【原创】pythonchalleng——第4题

来源:互联网 发布:mac pc区别 编辑:程序博客网 时间:2024/06/05 14:20

        第四题的网址:http://www.pythonchallenge.com/pc/def/linkedlist.php,如果大家访问不了该网站,可以看看下面的网站截图。

2011-07-17_110121

初看页面,什么提示都没有啊……这让人怎么做呀……不过我们做过第三题,其实还是知道哪里有可能含有提示:页面标题和页面源代码。标题显示的是“follow the chain”;页面源代码的提示如下:

urllib may help. DON'T TRY ALL NOTHINGS, since it will never end. 400 times is more than enough.

初看提示信息,不太明白NOTHING的含义。不过当我们的鼠标移动到图片上的时候,发现图片是可以点击的,点进去后,页面跳转到新的页面,显示的信息是:

and the next nothing is 44827

而且url中会传递一个参数nothing=12345看到这,大家应该明白nothing是什么意思了吧。提示中说不让我们挨个尝试nothing的值,因为那样的话是找不到出口的。那我们就跟着提示走,从12345跳转到44827,修改url后,再跳转到45439,我们再次修改url,这次跳转到的页面有趣,内容如下:

Your hands are getting tired and the next nothing is 94485

哈哈,像我们这样手动去寻找答案,的确挺累人的……提示信息里也说了,要尝试400次就会有答案了,但是那时候我们早累死了……好了,现在开始利用python解决问题。提示信息很给力,让我们常识使用urllib模块。没用过,不知道该怎么用?没关系,看看《python手册》就OK了。(不知道各位用的python都是什么版本的,所以给个列表大家自己选)

        找到urllib模块后,我们发现我们涉及到的函数就是

urlopen(url [ , data [ , proxies]])

Open a network object denoted by a URL for reading. If the URL does not have a scheme identifier, or if it has file: as its scheme identifier, this opens a local file (without universal newlines); otherwise it opens a socket to a server somewhere on the network. If the connection cannot be made the IOError exception is raised. If all went well, a file-like object is returned. This supports the following methods: read(), readline(), readlines(), fileno(), close(), info() andgeturl(). It also has proper support for the iterator protocol. One caveat: the read() method, if the size argument is omitted or negative, may not read until the end of the data stream; there is no good way to determine that the entire stream from a socket has been read in the general case.

好了,会使用urllib模块了,但是问题该怎么解决呢?我的思路就是利用上一题学习到的正则表达式来分析页面源代码,从中提取出数字,然后构造新的url,获取该url的源代码,直到找到我们需要的信息。这就是页面标题中说的“follow the chain”。见代码:

''' 
Created on 2011-7-17 
@author: hengha 
''' 
#-*- codeing:utf-8 -*- 
import urllib 
import re 
def getHtml(url): 
    filehandle = urllib.urlopen(url) 
    htmlCode=filehandle.read() 
    return htmlCode 
def getNext(str,rexRule): 
    p=re.compile(rexRule) 
    result=p.findall(str) 
    return result[0] 
def getNewUrl(url,number): 
    return url+number.__str__() 
     
if __name__ == '__main__': 
    url='http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=' 
    number=12345 
    newUrl=getNewUrl(url,number) 
    for i in range(500): 
        htmlCode=getHtml(newUrl) 
        print htmlCode 
        newUrl=getNewUrl(url,getNext(htmlCode,'[0-9]+')) 
        print newUrl 

为了便于跟踪,我们每个页面的url和源代码都显示出来了。好了,开始让我们的程序启动吧!

        程序跑了一会儿,就出现错误停止运行了!程序停止在这个url处:http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=16044。嘿嘿,看看是不是我们要的答案。页面内容居然是:

Yes. Divide by two and keep going.

还可以这样呀!本人能力有限,只能手动做除法,得到新的nothing参数8022。将源代码中的number修改为8022,再跑一次程序,继续期待!

        程序有运行了一会儿,终于出错停止了!看看是什么信息:

You've been misleaded to here. Go to previous one and check.

说我居然被误导了,返回上一个nothing看看是什么原因。(这里看到把信息打印出来的好处了吧!)

There maybe misleading numbers in the text. One example is 82683. Look only for the next nothing and the next nothing is 63579

这样也可以!我们重新修正nothing值为63579,再运行程序,不知道后面还会有什么问题!

        程序再次报错,不过这次我们终于得到我们期待的信息了,页面内容是:peak.html。将其替换到url中,成功进入第5题!

        作者不容易呀,还得给我们制造麻烦,不过我们还是顺利地进入第5题了。从这可以看出第4题为什么用php来写了。作者不想写这么多的静态页面,所以使用php构造动态页面来生成这些随机数。好了,准备准备,向着第5题出发!

0 0
原创粉丝点击