The Python Challenge(5)
2017-10-20 本文已影响0人
发条蛙
问题链接
问题链接如下:
http://www.pythonchallenge.com/pc/def/linkedlist.php
答案链接
答案链接如下:
http://www.pythonchallenge.com/pc/def/peak.html
解题思路
根据页面源码提示:
<!-- urllib may help. DON'T TRY ALL NOTHINGS, since it will never
end. 400 times is more than enough. -->
再点击页面图片显示:
and the next nothing is 44827
可知是需要一直跳转,则有如下代码:
from urllib import request
import re
url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
id = '12345'
while True:
response = request.urlopen(url+id)
content = str(response.read(), 'utf-8')
print(content)
ret = re.findall('and the next nothing is (\d+)', content)
if len(ret) == 0:
print(url+id)
break
id = ret[0]
显示到如下内容,然后停止:
and the next nothing is 16044
Yes. Divide by two and keep going.
则把代码中的初始ID替换为8022
,再次执行有如下结果:
and the next nothing is 66831
peak.html
则最终URL应当为:http://www.pythonchallenge.com/pc/def/peak.html
。