码迷,mamicode.com
首页 > 其他好文 > 详细

pychallenge(4)-follow the chain

时间:2017-03-17 23:40:39      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:value   elf   length   eterm   exce   ima   more   hal   ini   

pychallenge之四

技术分享

<!-- urllib may help. DON‘T TRY ALL NOTHINGS, since it will never 

end. 400 times is more than enough. -->

点图片跳出了:and the next nothing is 44827,看起来似乎是嵌套的网页

# -*- coding: utf-8 -*-
import requests
import re
import time

class NothingException(Exception):
    def __init__(self, length, rtvalue):
        Exception.__init__(self)
        self.length = length
        self.rtvalue = rtvalue

def findnothing(start_noth):
    """
    :type start_noth: str
    :rtype nothing : str
    """
    nothing = start_noth
    url_prefix = http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=
    url = url_prefix + nothing

    try:
        for i in range(400):
            r = requests.get(url)
            print "now it‘s %d time for requesting for page:%s" % (i, url)
            time.sleep(1)
            if r.status_code == 200:
                nothing = re.findall(and the next nothing is \d+, r.content) # 取出固定格式的目标字串
                if len(nothing) != 1:
                    raise NothingException(len(nothing), 1) # 不只一个结果的话说明文言有变动
                else:
                    url = url_prefix + str(re.findall(\d+, nothing[0])[0]) # 正常情况下取出nothing继续访问
            else:
                print "get page %s failed" % url
                break
    except NothingException:
        print "now it‘s necessary for you to determine <nothing>."
        print "hint is: %s" % r.content  
        m_noth = raw_input("please input <nothing>:")  # 非正常文言情况下需要手动确定nothing
        findnothing(m_noth)

    return nothing

if __name__ == __main__:
    print findnothing(12345)

最后结果是peak.html

 

pychallenge(4)-follow the chain

标签:value   elf   length   eterm   exce   ima   more   hal   ini   

原文地址:http://www.cnblogs.com/katsu/p/6569007.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!