Python Challenge (4)

这道题首先点击图片,发现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="
    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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值