这道题首先点击图片,发现URL变成“http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345”,下面一句话“and the next nothing is 92512”,因此把URL中等号后面的数字改成92512,发现有有新的next nothing。因此这道题就是把页面的数字提示更新到URL中。由于之前写过一个批量抓取网页内容的程序,因此这道题解决起来比较容易。
将程序写好,运行一段时间后发现,程序陷入循环。“and the next nothing is 92118 Yes. Divide by two and keep going.”因此需要把92118除以2,再放回URL的位置。
最后得到了结果是“PEAK.HTML”
URL:http://www.pythonchallenge.com/pc/def/peak.html
程序:
import urlparse
import urllib
import string
firsturl=http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345#此处需要替换一次,将92118除以2,结果46059放到这个位置继续运算。
fu=urllib.urlopen(firsturl)
text=fu.read()
while(True):
url="http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="
print text
if text.find("next nothing is"):
begin=text.find("next nothing is")+16
end=len(text)
num=text[begin:end]
newurl="".join([url,num])
u=urllib.urlopen(newurl)
text=u.read()
else:
print "Final Result: ",text
break
本文通过解析Python Challenge中的一道题目,详细介绍了如何使用Python脚本自动抓取网页内容并解析其中的线索,最终找到目标页面的过程。
1466

被折叠的 条评论
为什么被折叠?



