The Python Challenge(5)

问题链接

问题链接如下:

http://www.pythonchallenge.com/pc/def/linkedlist.php

答案链接

答案链接如下:

http://www.pythonchallenge.com/pc/def/peak.html

解题思路

根据页面源码提示:


再点击页面图片显示:

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

你可能感兴趣的:(The Python Challenge(5))