用Python标准库修改搜索引擎获取结果

开发 后端
Python标准库使用的过程中有不少的问题影响着我们的使用。下面我们就向大家介绍下简单的Python标准库详细使用方案。

Python标准库在长时间的使用中需要不断的学习。下面我们就看看如何才能更好的掌握相关的技术信息。希望对大家之后的使用和学习有所帮助。下面的就是想大家介绍下相关的使用方法。

我输入的关键字作为地址参数传递给某个程序,这个程序就会返回一个页面,上面包括顶部(logo和搜索UI)/结果部分/底部(版权信息部分),我们要得到的就是中间结果部分,这个可以用Python标准库的urllib中的urlopen方法得到整个页面的字符串,然后再解析这些字符串,完全有办法把中间结果部分抽取出来,抽出着串字符串,加上自己的头部和顶部和底部,那样搜索小偷的雏形就大概完成了,下面先写个测试代码。

  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]  

这里只是要求用户输入一个关键字,然后向搜索引擎提交请求,把返回的页面保存到一个目录下,这只是一个测试的例子,如果要做真正的搜索小偷,完全可以不保存这个页面,把抽取出来的字符串加入到我们预先设计好的模板里面,直接以web的形式显示在客户端,那样就可以实现利用盗取某些搜索引擎的结果并构造新的页面呈现。

看一下百度搜索结果页的源码,在搜索结构的那个table标签前面有个 <DIV id=Div> </DIV> 的标签,我们可以根据这个标签得到下移两行的结果集,于是增加一个方法。

  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]  

既然得到结果列表,那么我们要把这个结果列表放到自己定义的页面里面,我们可以说这个页面叫模板:

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

这里搜索的结构全都是百度那里过来的哦!其中%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]  

这样我们就可以得到一个自己定义的风格而含有百度搜索出来的结果的页面,这里只做了标题和结果及的替换,同样道理,我们还可以把“百度快照”替换掉,我们还可以重新生成翻页控件,这样一个搜索小偷就基本完成啦。

用Python标准库向Google请求时,Google会返回一个不是我们希望得到的页面,上面的内容是提示无权访问,Google很聪明,这步已经被他们想到了,但百度没做这样的限制哦,于是成功截取百度的数据。同样道理,还可以尝试其他搜索引擎,比如yisou和soso。

做个自己的页面风格,盗取baidu的搜索结果,打造自己的品牌而利用别人的数据,甚至去掉baidu的广告加上自己的广告,这种做法实在是太不厚道了,哈哈哈。该程序只为学习python标准库用,具体来说没什么意义。
 

【编辑推荐】

  1. Python列表与C#语言的相似度介绍
  2. Python字符串的广泛应用
  3. Python抓取的具体应用解答
  4. Python字符串类型的详细介绍
  5. Python列表内涵实际中的使用介绍
责任编辑:张浩 来源: 人民邮电出版社
相关推荐

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

2017-11-27 13:39:29

Python大数据搜索引擎

2012-09-07 13:22:21

搜索搜狗

2022-10-08 09:13:18

搜索引擎⽹站

2009-09-22 16:23:52

搜索引擎

2024-02-27 07:33:32

搜索引擎Rust模型

2015-08-17 10:34:30

2011-05-17 16:54:09

搜索引擎

2019-07-10 13:17:07

大数据搜索代码

2011-06-03 10:19:53

2009-12-10 15:09:46

PHP搜索引擎类

2023-09-21 15:05:12

ChatGPT搜索引擎

2016-12-26 13:41:19

大数据搜索引擎工作原理
点赞
收藏

51CTO技术栈公众号