• <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>

            清風(fēng)竹林

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

            Python Challenge lv4: follow the chain

              題目鏈接: http://www.pythonchallenge.com/pc/def/linkedlist.php
               
              說實話,好不容易通過google搞清楚題目的要求: 通過不斷的從服務(wù)器取得一個web page,然后從源碼中找出下一個鏈接的地址。需要注意的是:雖然頁面的源碼很簡單,但并不是其中所有的數(shù)字都是有效的,需要使用正則表達(dá)式找出正確的pattern形式才可以,對本題而言r'nothing is (\d+)'是一個可用的pattern,使用''.join([x for x in text if x.isdigit()] 將所有的數(shù)字都粘連起來了,結(jié)果跟蹤到4000多還沒結(jié)束,才知道上當(dāng)了。。。
             
            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,題目中最早給出的可不是這個值, 我是從地址列表的后一部分選了一個數(shù)字而已,因此不要擔(dān)心)

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

            評論

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

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

            # re: Python Challenge lv4: follow the chain 2011-06-01 10:04 李現(xiàn)民

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

            久久国产欧美日韩精品| 日本精品久久久久中文字幕8| 伊人久久综合精品无码AV专区| 久久香蕉国产线看观看乱码| 区久久AAA片69亚洲| 韩国无遮挡三级久久| 欧美一区二区久久精品| 久久久久亚洲AV综合波多野结衣| 伊人久久大香线蕉综合影院首页| 久久久久亚洲AV成人网| 久久综合给久久狠狠97色| 亚洲国产精品综合久久一线 | 亚洲精品美女久久777777| 久久99热这里只有精品国产| 成人国内精品久久久久影院| 亚洲级αV无码毛片久久精品| 人人妻久久人人澡人人爽人人精品| 精品久久久久久99人妻| 久久中文字幕一区二区| 国产午夜精品理论片久久影视| 久久国产亚洲高清观看| 亚洲狠狠婷婷综合久久久久| 亚洲国产精品无码久久久秋霞2| 久久久精品久久久久影院| 亚洲性久久久影院| 久久午夜夜伦鲁鲁片免费无码影视| 亚洲精品NV久久久久久久久久| 久久精品国产国产精品四凭| 久久久久综合国产欧美一区二区| 国产毛片久久久久久国产毛片 | 色8久久人人97超碰香蕉987| 99久久无色码中文字幕人妻| 中文字幕无码免费久久| 中文国产成人精品久久不卡| 一本久久知道综合久久| 亚洲国产精品无码久久SM| 久久亚洲美女精品国产精品| 99国产精品久久久久久久成人热| 国内精品久久久久影院免费| 模特私拍国产精品久久| 亚洲欧洲精品成人久久奇米网|