Python3 解谜挑战 关卡四

作者: AiFany | 来源:发表于2018-01-18 14:38 被阅读62次
    PythonChallenge.png
    查看上一关过关方法

    关卡四:follow the chain 循迹前行

    4.png

    提示
       此题的题目“follow the chain”,图片中显示的正好是一个链。首先还是查看一下源文件,寻找线索。

    tips.png

    根据上图红框文字:利用库urllib(Python 的爬虫库), 以及兰框文字“linkenlist.php?nothing=12345”。在浏览器地址加入?nothing=12345, 看会发生什么?

    next.png

    页面中出现了另一个数字44827,这就是解决此关的思路。注意不要一直循环下去(“DON’T TRY ALL NOTHINGS” ),因为网页源文件红框文字中提到了400次。

    开始解谜:
      python3解答

    import urllib.request as urlre#引入库
    times = 1
    startnumber = '12345'
    address = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
    while times < 400:
        sourcepage = urlre.urlopen(address + startnumber)
        source = sourcepage.read()
        sourcepage.close()
        #获得源码中的数字
        num = ''
        for jj in source:
            try:
                int(chr(jj))
                num += chr(jj)
            except ValueError:
                pass
        if num  == '':#没有搜索到数字
            print(source)#打印源码
            break
        startnumber = num
        times += 1
    

      程序会突然中断,因为并不是每个页面都有数字,程序运行结果:

    16044 85
    b'Yes. Divide by two and keep going.'
    

    从上面文字看出,需要除以2,也就是将16044/2=8022,作为下一个数字。程序更改如下:

    times = 1
    startnumber = '12345'
    address = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
    while times < 400:
        sourcepage = urlre.urlopen(address + startnumber)
        source = sourcepage.read()
        sourcepage.close()
        print(source)
        #获得源码中的数字
        num = ''
        for jj in source:
            try:
                int(chr(jj))
                num += chr(jj)
            except ValueError:
                pass
        if num  == '':#没有搜索到数字
            startnumber = str(int(int(startnumber) / 2))#除以2
        else:
            startnumber = num
        times += 1
    

      从上面打印出的源码,可发现当nothing=82682时,页面内容变为:
    'There maybe misleading numbers in the \ntext. One example is 82683. Look only for the next nothing and the next nothing is 63579'
    因此还需要更改程序如下:

    times = 1
    startnumber = '12345'
    address = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
    while times < 400:
        sourcepage = urlre.urlopen(address + startnumber)
        source = sourcepage.read()
        sourcepage.close()
        print(source)
        #获得源码中的数字
        num = ''
        for jj in source:
            try:
                int(chr(jj))
                num += chr(jj)
            except ValueError:
                num = '' #只选取源码最后的数字
        if num  == '':#没有搜索到数字
            startnumber = str(int(int(startnumber) / 2))#除以2
        else:
            startnumber = num
        print(startnumber, times)
        times += 1
    
    

    从上面打印出的源码,可发现当nothing=66831时,页面内容变为:'peak.html'。因此还需要更改程序如下:

    times = 1
    startnumber = '27709'
    address = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
    while times < 400:
        sourcepage = urlre.urlopen(address + startnumber)
        source = sourcepage.read()
        sourcepage.close()
        #获得源码中的数字
        num = ''
        for jj in source:
            try:
                int(chr(jj))
                num += chr(jj)
            except ValueError:
                num = '' #只选取源码最后的数字
        if '.html' in str(source):
            print(str(source)[2:-1])
            break
        else:
            if num  == '':#没有搜索到数字
                startnumber = str(int(int(startnumber) / 2))#除以2
            else:
                startnumber = num
        times += 1
    

      最终的输出结果为:peak.html
      程序运行期间有时会报错: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。这是因为网络连接的问题,因此需要设置连接的时间,需要在程序前加上。

    import socket
    socket.setdefaulttimeout(60)#设置最长的等待连接时间为1分钟
    

      最后将网页地址中的linkedlist.php换为peak.html即可进入下一关

    不定期更新,欢迎留言,敬请关注!!!

    相关文章

      网友评论

        本文标题:Python3 解谜挑战 关卡四

        本文链接:https://www.haomeiwen.com/subject/bqcwoxtx.html