成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

用Python標準庫修改搜索引擎獲取結(jié)果

開發(fā) 后端
Python標準庫使用的過程中有不少的問題影響著我們的使用。下面我們就向大家介紹下簡單的Python標準庫詳細使用方案。

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

我輸入的關(guān)鍵字作為地址參數(shù)傳遞給某個程序,這個程序就會返回一個頁面,上面包括頂部(logo和搜索UI)/結(jié)果部分/底部(版權(quán)信息部分),我們要得到的就是中間結(jié)果部分,這個可以用Python標準庫的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標簽前面有個 <DIV id=Div> </DIV> 的標簽,我們可以根據(jù)這個標簽得到下移兩行的結(jié)果集,于是增加一個方法。

  1. getResultStr()   
  2. [code]   
  3. def getResultStr(self,webStr):   
  4. webStrwebStrList = webStr.read().split( "\r\n ")   
  5. line = webStrList.index( " <DIV id=Div> </DIV> ")+2 # get the line from " <DIV id=Div> </DIV> " move 2 line   
  6. resultStr = webStrList[line]   
  7. return resultStr   
  8. [/code]  

既然得到結(jié)果列表,那么我們要把這個結(jié)果列表放到自己定義的頁面里面,我們可以說這個頁面叫模板:

  1. [code]   
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">   
  3. <html xmlns"http://www.w3.org/1999/xhtml ">   
  4. <head>   
  5. < http-equivhttp-equiv"Content-Type " content"text/html; charset=gb2312 " />   
  6. <title> SuperSingo搜索-%title% </title>   
  7. <link href"default/css/global.css " type=text/css rel=stylesheet>   
  8. </head>   
  9. <body>   
  10. <div id"top ">   
  11. <div id"logo "> <img src"default/images/logo.jpg " /> </div>   
  12. <div id"searchUI ">   
  13. <input type"text " style"width:300px; " />   
  14. <input type"submit " value"Search " />   
  15. </div>   
  16. <div class"clear "/>   
  17. </div>   
  18. <div id"result_info ">   
  19. 工找到:×××條記錄,耗時×××秒   
  20. </div>   
  21. <div id"result "> %result% </div>   
  22. <div id"foot ">  

這里搜索的結(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%替換成我們想要的標簽了。

  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( " <DIV id=Div> </DIV> ")+2 # get the line from " <DIV id=Div> </DIV> " move 2 line   
  24. resultStr = webStrList[line]   
  25. return resultStr   
  26. def reCreatePage(self,resultStr):   
  27. demoStr = urllib.urlopen(demoPage).read() # get the demo page string   
  28. rereTitle = re.compile( "%title% ")   
  29. demoStr = reTitle.sub(self.key,demoStr) # re set the page title   
  30. rereResult = re.compile( "%result% ")   
  31. demoStr = reResult.sub(resultStr,demoStr) # re set the page result   
  32. return demoStr   
  33. def setPageToFile(self,webStr):   
  34. rereSetStr = re.compile( "\r ")   
  35. self.key = reSetStr.sub( " ",self.key) # replace the string "\r "   
  36. targetFile = file(path+self.key+ ".html ", "w ") # open the file for "w "rite   
  37. targetFile.write(webStr)   
  38. targetFile.close()   
  39. print "done "   
  40. inputKey = raw_input( "Enter you want to search --> ")   
  41. obj = SearchThief(inputKey)   
  42. obj.getPage()   
  43. [/code]  

這樣我們就可以得到一個自己定義的風(fēng)格而含有百度搜索出來的結(jié)果的頁面,這里只做了標題和結(jié)果及的替換,同樣道理,我們還可以把“百度快照”替換掉,我們還可以重新生成翻頁控件,這樣一個搜索小偷就基本完成啦。

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

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

【編輯推薦】

  1. Python列表與C#語言的相似度介紹
  2. Python字符串的廣泛應(yīng)用
  3. Python抓取的具體應(yīng)用解答
  4. Python字符串類型的詳細介紹
  5. Python列表內(nèi)涵實際中的使用介紹
責(zé)任編輯:張浩 來源: 人民郵電出版社
相關(guān)推薦

2010-03-11 19:06:52

Python編程語言

2016-08-18 00:54:59

Python圖片處理搜索引擎

2011-06-20 18:23:06

SEO

2011-07-21 16:32:07

SEO

2020-03-20 10:14:49

搜索引擎倒排索引

2017-08-07 08:15:31

搜索引擎倒排

2009-02-19 09:41:36

搜索引擎搜狐百度

2010-04-20 11:43:46

2022-10-08 09:13:18

搜索引擎?站

2012-09-07 13:22:21

搜索搜狗

2009-09-22 16:23:52

搜索引擎

2017-11-27 13:39:29

Python大數(shù)據(jù)搜索引擎

2015-08-17 10:34:30

2024-02-27 07:33:32

搜索引擎Rust模型

2011-05-17 16:54:09

搜索引擎

2019-07-10 13:17:07

大數(shù)據(jù)搜索代碼

2009-12-10 15:09:46

PHP搜索引擎類

2023-09-21 15:05:12

ChatGPT搜索引擎

2016-12-26 13:41:19

大數(shù)據(jù)搜索引擎工作原理

2011-06-03 10:19:53

點贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 在线看91 | 久久久精品影院 | 中文字幕国产高清 | 精品视频网 | 在线综合视频 | 精品久久国产 | 精品欧美一区二区三区久久久小说 | 91视视频在线观看入口直接观看 | 欧美一区二区三区在线观看视频 | 一区二区在线看 | 国产精品久久久久无码av | 丁香婷婷久久久综合精品国产 | 亚洲免费在线 | 欧美精品一区二区免费 | 国产精品一区二区视频 | 男女羞羞视频在线观看 | 欧美一区视频在线 | 色欧美片视频在线观看 | 精品1区2区| 日本不卡一区 | 久久影院一区 | 亚洲欧美日韩电影 | 久久久性色精品国产免费观看 | 一级女毛片| 久久国产一区二区三区 | 国产精品久久久亚洲 | 一区二区日韩 | 综合激情久久 | 免费观看一级毛片 | 国产乱码精品一区二区三区忘忧草 | 91免费观看国产 | 热re99久久精品国99热观看 | 日本精品一区二区在线观看 | av黄色在线 | 欧美精品第三页 | 尤物在线 | 精品一区二区三区四区五区 | 在线看黄免费 | 亚洲精品一区二区网址 | 成人在线免费视频 | 国产乱码精品一区二区三区五月婷 |