[Level 4]
Title: follow the chain
页面中无有用的提示,查看源码,在注释中找到
urllib may help. DON'T TRY ALL NOTHINGS, since it will never end. 400 times is more than enough.
且图片是一个链接,点击图片,跳转到http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345,网页显示and the next nothing is 44827。可以想到是修改url中的nothing值直至找到答案。
import urllib.request,re
url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
num = '12345'
pattern = re.compile(r'.*and the next nothing is (\d+)')
while num:
data = urllib.request.urlopen(url+num).read().decode('utf-8')
num = ''.join(pattern.findall(data))
print(data)
运行结束,显示Yes. Divide by two and keep going.,此时的num=16044,num=num/2=8022,修改后继续运行while循环(中间出现过一个干扰项),得到最终结果是peak.html,[Level 5]
实际中使用urllib.request.urlopen()
处理速度较慢,且经常死,换用第三方库(如httplib2)效果大赞。
import httplib2
data = httplib2.Http().request(url+num)[1].decode('utf-8')
httplib2.Http().request()
返回Response
实例和内容组成的元组。
Python Challenge Wiki
获取nothing值和其他方法:
text.strip().split()[-1]