日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
用Python標(biāo)準(zhǔn)庫修改搜索引擎獲取結(jié)果

Python標(biāo)準(zhǔn)庫在長時間的使用中需要不斷的學(xué)習(xí)。下面我們就看看如何才能更好的掌握相關(guān)的技術(shù)信息。希望對大家之后的使用和學(xué)習(xí)有所幫助。下面的就是想大家介紹下相關(guān)的使用方法。

公司主營業(yè)務(wù):成都網(wǎng)站設(shè)計、成都做網(wǎng)站、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出東光免費做網(wǎng)站回饋大家。

我輸入的關(guān)鍵字作為地址參數(shù)傳遞給某個程序,這個程序就會返回一個頁面,上面包括頂部(logo和搜索UI)/結(jié)果部分/底部(版權(quán)信息部分),我們要得到的就是中間結(jié)果部分,這個可以用Python標(biāo)準(zhǔn)庫的urllib中的urlopen方法得到整個頁面的字符串,然后再解析這些字符串,完全有辦法把中間結(jié)果部分抽取出來,抽出著串字符串,加上自己的頭部和頂部和底部,那樣搜索小偷的雛形就大概完成了,下面先寫個測試代碼。

 
 
 
  1. [code]   
  2. # Search Thief   
  3. # creator: Singo   
  4. # date: 2007-8-24   
  5. import urllib   
  6. import re   
  7. class SearchThief:   
  8. " " "the google thief " " "   
  9. global path,targetURL   
  10. path = "pages\\ "   
  11. # targetURL = "http://www.google.cn/search?complete=1&hl=zh-CN&q= "   
  12. targetURL = "http://www.baidu.com/s?wd= "   
  13. def __init__(self,key):   
  14. self.key = key   
  15. def getPage(self):   
  16. webStr = urllib.urlopen(targetURL+self.key).read() # get the page string form the url   
  17. self.setPageToFile(webStr)   
  18. def setPageToFile(self,webStr):   
  19. rereSetStr = re.compile( "\r ")   
  20. self.key = reSetStr.sub( " ",self.key) # replace the string "\r "   
  21. targetFile = file(path+self.key+ ".html ", "w ") # open the file for "w "rite   
  22. targetFile.write(webStr)   
  23. targetFile.close()   
  24. print "done "   
  25. inputKey = raw_input( "Enter you want to search --> ")   
  26. obj = SearchThief(inputKey)   
  27. obj.getPage()   
  28. [/code]  

這里只是要求用戶輸入一個關(guān)鍵字,然后向搜索引擎提交請求,把返回的頁面保存到一個目錄下,這只是一個測試的例子,如果要做真正的搜索小偷,完全可以不保存這個頁面,把抽取出來的字符串加入到我們預(yù)先設(shè)計好的模板里面,直接以web的形式顯示在客戶端,那樣就可以實現(xiàn)利用盜取某些搜索引擎的結(jié)果并構(gòu)造新的頁面呈現(xiàn)。

看一下百度搜索結(jié)果頁的源碼,在搜索結(jié)構(gòu)的那個table標(biāo)簽前面有個

的標(biāo)簽,我們可以根據(jù)這個標(biāo)簽得到下移兩行的結(jié)果集,于是增加一個方法。

 
 
 
  1. getResultStr()   
  2. [code]   
  3. def getResultStr(self,webStr):   
  4. webStrwebStrList = webStr.read().split( "\r\n ")   
  5. line = webStrList.index( "  
 ")+2 # get the line from "  
 " move 2 line   
  • resultStr = webStrList[line]   
  • return resultStr   
  • [/code]  
  • 既然得到結(jié)果列表,那么我們要把這個結(jié)果列表放到自己定義的頁面里面,我們可以說這個頁面叫模板:

     
     
     
    1. [code]   
    2.    
    3.    
    4.    
    5. < http-equivhttp-equiv= "Content-Type " content= "text/html; charset=gb2312 " />   
    6.  SuperSingo搜索-%title%    
    7.    
    8.    
    9.    
    10.    
    11.   
       
  •    
  •    
  •    
  •    
  •    
  •    
  •    
  • 工找到:×××條記錄,耗時×××秒   
  •    
  •  %result% 
  •    
  •   
  • 這里搜索的結(jié)構(gòu)全都是百度那里過來的哦!其中%title%和%result%是等待替換的字符,為了替換這些字符,我們再增加一個方法, #p#

     
     
     
    1. [b]reCreatePage():[/b]   
    2. [code]   
    3. def reCreatePage(self,resultStr):   
    4. demoStr = urllib.urlopen(demoPage).read() # get the demo page string   
    5. rereTitle = re.compile( "%title% ")   
    6. demoStr = reTitle.sub(self.key,demoStr) # re set the page title   
    7. rereResult = re.compile( "%result% ")   
    8. demoStr = reResult.sub(resultStr,demoStr) # re set the page result   
    9. return demoStr   
    10. [/code]  

    這樣就可以把模板中的%title%和%result%替換成我們想要的標(biāo)簽了。

     
     
     
    1. [code]   
    2. # the main programme   
    3. # creator: Singo   
    4. # date: 2007-8-24   
    5. import urllib   
    6. import re   
    7. class SearchThief:   
    8. " " "the google thief " " "   
    9. global path,targetURL,demoPage   
    10. path = "pages\\ "   
    11. # targetURL = "http://www.google.cn/search?complete=1&hl=zh-CN&q= "   
    12. targetURL = "http://www.baidu.com/s?wd= "   
    13. demoPage = path+ "__demo__.html "   
    14. def __init__(self,key):   
    15. self.key = key   
    16. def getPage(self):   
    17. webStr = urllib.urlopen(targetURL+self.key) # get the page string form the url   
    18. webStr = self.getResultStr(webStr) # get the result part   
    19. webStr = self.reCreatePage(webStr) # re create a new page   
    20. self.setPageToFile(webStr)   
    21. def getResultStr(self,webStr):   
    22. webStrwebStrList = webStr.read().split( "\r\n ")   
    23. line = webStrList.index( "  
     ")+2 # get the line from "  
     " move 2 line   
  • resultStr = webStrList[line]   
  • return resultStr   
  • def reCreatePage(self,resultStr):   
  • demoStr = urllib.urlopen(demoPage).read() # get the demo page string   
  • rereTitle = re.compile( "%title% ")   
  • demoStr = reTitle.sub(self.key,demoStr) # re set the page title   
  • rereResult = re.compile( "%result% ")   
  • demoStr = reResult.sub(resultStr,demoStr) # re set the page result   
  • return demoStr   
  • def setPageToFile(self,webStr):   
  • rereSetStr = re.compile( "\r ")   
  • self.key = reSetStr.sub( " ",self.key) # replace the string "\r "   
  • targetFile = file(path+self.key+ ".html ", "w ") # open the file for "w "rite   
  • targetFile.write(webStr)   
  • targetFile.close()   
  • print "done "   
  • inputKey = raw_input( "Enter you want to search --> ")   
  • obj = SearchThief(inputKey)   
  • obj.getPage()   
  • [/code]  
  • 這樣我們就可以得到一個自己定義的風(fēng)格而含有百度搜索出來的結(jié)果的頁面,這里只做了標(biāo)題和結(jié)果及的替換,同樣道理,我們還可以把“百度快照”替換掉,我們還可以重新生成翻頁控件,這樣一個搜索小偷就基本完成啦。

    用Python標(biāo)準(zhǔn)庫向Google請求時,Google會返回一個不是我們希望得到的頁面,上面的內(nèi)容是提示無權(quán)訪問,Google很聰明,這步已經(jīng)被他們想到了,但百度沒做這樣的限制哦,于是成功截取百度的數(shù)據(jù)。同樣道理,還可以嘗試其他搜索引擎,比如yisou和soso。

    做個自己的頁面風(fēng)格,盜取baidu的搜索結(jié)果,打造自己的品牌而利用別人的數(shù)據(jù),甚至去掉baidu的廣告加上自己的廣告,這種做法實在是太不厚道了,哈哈哈。該程序只為學(xué)習(xí)python標(biāo)準(zhǔn)庫用,具體來說沒什么意義。


    本文名稱:用Python標(biāo)準(zhǔn)庫修改搜索引擎獲取結(jié)果
    本文URL:http://m.5511xx.com/article/ccssogo.html