我晕,我对网页确实不了解,段落的缩进经常失败,编辑了几次了……
网址:http://www.pythonchallenge.com/pc/def/linkedlist.php
不管是查看源代码还是直接点网页,都可以看到http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345 及 网页中and the next nothing is 44827。
题意搞清楚了,就是把后面的数字替换原地址中的数字,过程中肯定不会一帆风顺,但是,没办法预测到会出什么问题,只能见招拆招,见人说鬼话了……
最开始,我是用正则表达式的,很简单,用re.compile('[0-9]+') 来匹配数字。
有两个地方有所变化。
第一个坎是‘Yes. Divide by two and keep going.',这样,就加上了index = text.find("two"),如果找到two,则当前数字除2。
第二个坎也是最后一个坎,There maybe misleading numbers in the text. One example is 82683. Look only for the next nothing and the next nothing is 63579。
我最开始的正则表达式很给面子,肯定是82683,63579,就是不知道这个往下会如何。
这里提示只查找"the next nothing",所以,其实正则表达式可以变一下。把这句话也加进模式中。
最后的代码是:
import re
import urllib2
url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="
number = "49574"
count = 1
while True:
file = urllib2.urlopen(url+number)
text = file.read()
print text
numbers = re.findall(r'nothing is ([0-9]+)', text)
if text.find('two') != -1:
number = str(int(number)/2)
print "result is " + number
continue
if len(numbers) == 0:
break
number = ''.join(numbers)
print number
file.close()
这里file.close()也不知意义几何。
另外,大概是网速原因,运行速度很慢……最后好像要查找100多还是200个文件,才最终看到peak.html的网址。