PythonChallenge.png
查看上一关过关方法
关卡四: follow the chain 循迹前行
4.png
提示:
此题的题目 follow the chain, 图片中显示的正好是一个链首先还是查看一下源文件, 寻找线索
tips.png
根据上图红框文字: 利用库 urllib(Python 的爬虫库), 以及兰框文字 linkenlist.php?nothing=12345 在浏览器地址加入? nothing=12345, 看会发生什么?
next.png
页面中出现了另一个数字 44827, 这就是解决此关的思路注意不要一直循环下去 (DONT 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 即可进入下一关
不定期更新, 欢迎留言, 敬请关注!!!
来源: http://www.jianshu.com/p/0cfa44e7d29e