• <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
               
              說(shuō)實(shí)話,好不容易通過(guò)google搞清楚題目的要求: 通過(guò)不斷的從服務(wù)器取得一個(gè)web page,然后從源碼中找出下一個(gè)鏈接的地址。需要注意的是:雖然頁(yè)面的源碼很簡(jiǎn)單,但并不是其中所有的數(shù)字都是有效的,需要使用正則表達(dá)式找出正確的pattern形式才可以,對(duì)本題而言r'nothing is (\d+)'是一個(gè)可用的pattern,使用''.join([x for x in text if x.isdigit()] 將所有的數(shù)字都粘連起來(lái)了,結(jié)果跟蹤到4000多還沒(méi)結(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

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

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

            評(píng)論

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

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

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

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

            久久99久久99精品免视看动漫| 国产99精品久久| 无码人妻久久一区二区三区蜜桃| 国产精品欧美亚洲韩国日本久久 | 亚洲天堂久久久| 欧美一区二区三区久久综合| 日韩亚洲欧美久久久www综合网| 精品99久久aaa一级毛片| 亚洲国产精品18久久久久久| 久久伊人精品青青草原高清| 久久精品视频一| 曰曰摸天天摸人人看久久久| 久久精品免费一区二区| 欧美精品一本久久男人的天堂| 久久精品国产清自在天天线| 免费国产99久久久香蕉| 国产美女久久精品香蕉69| 久久婷婷五月综合97色直播| 久久av高潮av无码av喷吹| 久久噜噜电影你懂的| 青青草原精品99久久精品66| 少妇熟女久久综合网色欲| 精品久久久久久久久久久久久久久| 麻豆成人久久精品二区三区免费| 亚洲国产成人精品久久久国产成人一区二区三区综 | 狼狼综合久久久久综合网| 免费久久人人爽人人爽av| 午夜视频久久久久一区| 伊人久久一区二区三区无码| 99热精品久久只有精品| 亚洲国产精品热久久| 久久被窝电影亚洲爽爽爽| 国产精品久久自在自线观看| .精品久久久麻豆国产精品| 精品999久久久久久中文字幕| 久久精品国产亚洲AV无码娇色| 久久精品国产精品亚洲精品| 日产精品99久久久久久| 精品国产乱码久久久久久郑州公司| 久久水蜜桃亚洲av无码精品麻豆 | 热综合一本伊人久久精品 |