• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            清風竹林

            ぷ雪飄絳梅映殘紅
               ぷ花舞霜飛映蒼松
                 ----- Do more,suffer less

            Python Challenge lv4: follow the chain

              題目鏈接: http://www.pythonchallenge.com/pc/def/linkedlist.php
               
              說實話,好不容易通過google搞清楚題目的要求: 通過不斷的從服務器取得一個web page,然后從源碼中找出下一個鏈接的地址。需要注意的是:雖然頁面的源碼很簡單,但并不是其中所有的數字都是有效的,需要使用正則表達式找出正確的pattern形式才可以,對本題而言r'nothing is (\d+)'是一個可用的pattern,使用''.join([x for x in text if x.isdigit()] 將所有的數字都粘連起來了,結果跟蹤到4000多還沒結束,才知道上當了。。。
             
            import re
            import urllib.request


            if __name__ == '__main__':
                url 
            = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
                index 
            = '17675'
                counter 
            = 1
                pattern 
            = re.compile(r'nothing is (\d+)')
                
            while True:
                    
            try:
                        request
            = urllib.request.Request(url+index)
                        
            # my pc must use proxy to connect
                        request.set_proxy('172.16.0.252:80''http')
                        response
            = urllib.request.urlopen(request) 
                        content
            =str(response.read().decode())
                        response.close()    
                        
            print(counter, content)
                        
                        result 
            = pattern.search(content)
                        
            if not result:
                            
            break
                        
                        index 
            = result.group(1)
                        counter 
            += 1        
                    
            except Exception as ex:
                        
            print(ex)
                        
            break
              程序輸出:
            1 and the next nothing is 8511
            2 and the next nothing is 89456
            3 and the next nothing is 43502
            4 and the next nothing is 45605
            5 and the next nothing is 12970
            6 and the next nothing is 91060
            7 and the next nothing is 27719
            8 and the next nothing is 65667
            9 peak.html

            得到下一個題目的地址peak.html (注:我的index初始值是17675,題目中最早給出的可不是這個值, 我是從地址列表的后一部分選了一個數字而已,因此不要擔心)

            posted on 2009-05-11 16:05 李現民 閱讀(570) 評論(2)  編輯 收藏 引用 所屬分類: python

            評論

            # re: Python Challenge lv4: follow the chain[未登錄] 2011-05-31 20:17 simon

            有個問題想問:
            你是如何知道要用nothing去做pattern的呢?  回復  更多評論   

            # re: Python Challenge lv4: follow the chain 2011-06-01 10:04 李現民

            @simon
            你去看一個網頁的源代碼,里面有一個鏈接, 你點一下會出現 and the next nothing is 92512, 替換Url里linkedlist.php?nothing=12345中的12345,然后再回車, 你就會發現規律了  回復  更多評論   

            中文字幕久久精品无码| 2021久久国自产拍精品| 一级做a爰片久久毛片人呢| 久久91亚洲人成电影网站| 国产高清美女一级a毛片久久w| 久久99亚洲综合精品首页| 色综合久久天天综线观看| 亚洲国产精品无码久久98| 色综合色天天久久婷婷基地| 欧美日韩精品久久免费| 久久综合久久久| 91久久精品电影| 久久97久久97精品免视看| 精品久久久久久国产牛牛app| 狠狠色综合久久久久尤物| 欧美精品丝袜久久久中文字幕 | 精品99久久aaa一级毛片| 狠狠久久综合| 久久婷婷五月综合色奶水99啪 | 久久婷婷国产剧情内射白浆| 欧美精品九九99久久在观看| 色综合久久久久网| 久久久久亚洲av毛片大| 97精品依人久久久大香线蕉97| 久久久免费精品re6| 伊人久久无码中文字幕| 97r久久精品国产99国产精| 久久人妻少妇嫩草AV蜜桃| 亚洲国产另类久久久精品| 久久青青草原国产精品免费| 久久一区二区三区99| 精品永久久福利一区二区| 欧美一区二区三区久久综合| 国产成人精品综合久久久| 精品国产一区二区三区久久| 亚洲精品乱码久久久久久不卡| 日韩十八禁一区二区久久| 狠狠色丁香久久婷婷综合五月 | 久久久黄片| 色婷婷久久久SWAG精品| AV无码久久久久不卡蜜桃|