Python Challenge Level 3 ~ 4

Level 3: 

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

根据hints,打开page source code,查找前后各仅有3个大写字母包围的小写字母。

#!/usr/bin/python  
  
import urllib.request  
import re  
  
""" 
Python Challenge, Level 3: 
    http://www.pythonchallenge.com/pc/def/equality.html
"""  
  
def main():  
    url = 'http://www.pythonchallenge.com/pc/def/equality.html'  
    resp = urllib.request.urlopen(url).read().decode()  
    match = re.search(r'<!--(.*?)-->', resp, re.DOTALL)  
    result = ''.join(re.findall(r'[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]', match.group(1)))  
      
    print(result)  
      
if __name__ == '__main__':  
  main()  

 得到结果linkedlist,还是老办法,替换url,进入下一关。

 

Level 4:

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

图没懂,其实就是个链接,具体打开page source code可以看到。链接为一个php的页面,带有参数nothing,访问可以得到信息“and the next nothing is 44827”,参数值迭代更新;编码追踪,直至找到第一个非此类信息。

#!/usr/bin/python  
  
import urllib.request  
import re  
  
""" 
Python Challenge, Level 4: 
    http://www.pythonchallenge.com/pc/def/linkedlist.php
"""  

def pingurl(common_url, initial):  
    while(initial):
        url = common_url + initial
        resp = urllib.request.urlopen(url).read().decode()
        match = re.search(r'and the next nothing is (\d+)', resp)    
        if match :
            initial = match.group(1)
            print(initial)
        else :
            return resp 
        
def main():  
    url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
    initial = '12345'
    print(pingurl(url, initial)) 
      
if __name__ == '__main__':  
  main()  

 初始值12345,若干次访问后得到信息“Yes. Divide by two and keep going.”;对最后一个值16044整除2,作为新的初始值继续,最终得到peak.html。替换url,进入下一关:)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值