设为首页 收藏本站
查看: 1353|回复: 0

[经验分享] python 小偷程序

[复制链接]

尚未签到

发表于 2015-4-23 07:29:12 | 显示全部楼层 |阅读模式
  分析了一些Google和百度的地址参数,比如我要搜索一个关键字“SuperSingo”,我在输入框输入SuperSingo并点击搜索后,地址栏变为:   
Google:http://www.google.cn/search?complete=1&hl=zh-CN&q=SuperSingo&=   
Baidu:http://www.baidu.com/s?wd=SuperSingo&cl=3   
也就是说,我输入的关键字作为地址参数传递给某个程序,这个程序就会返回一个页面,上面包括顶部(logo和搜索UI)/结果部分/底部(版权信息部分),我们要得到的就是中间结果部分,这个可以用Python标准库的urllib中的urlopen方法得到整个页面的字符串,然后再解析这些字符串,完全有办法把中间结果部分抽取出来,抽出着串字符串,加上自己的头部和顶部和底部,那样搜索小偷的雏形就大概完成了,下面先写个测试代码。   
代码   
  1.    
  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.                 reSetStr   =   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()   
复制代码
   
这里只是要求用户输入一个关键字,然后向搜索引擎提交请求,把返回的页面保存到一个目录下,这只是一个测试的例子,如果要做真正的搜索小偷,完全可以不保存这个页面,把抽取出来的字符串加入到我们预先设计好的模板里面,直接以web的形式显示在客户端,那样就可以实现利用盗取某些搜索引擎的结果并构造新的页面呈现。   
继续:   
看一下百度搜索结果页的源码,在搜索结构的那个table标签前面有个   的标签,我们可以根据这个标签得到下移两行的结果集,于是增加一个方法getResultStr()   
  1.    
  2. def   getResultStr(self,webStr):   
  3.         webStrList   =   webStr.read().split( "\r\n ")   
  4.         line   =   webStrList.index( "   ")+2     #   get   the   line   from   "   "   move   2   line   
  5.         resultStr   =   webStrList[line]   
  6.         return   resultStr   
复制代码
   
既然得到结果列表,那么我们要把这个结果列表放到自己定义的页面里面,我们可以说这个页面叫模板:   
  1.    
  2.    
  3.    
  4.    
  5. <   http-equiv= &quot;Content-Type &quot;   content= &quot;text/html;   charset=gb2312 &quot;   />   
  6. SuperSingo搜索-%title%     
  7.    
  8.    
  9.    
  10.    
  11.       
  12.             
  13.             
  14.                     
  15.             
  16.             
  17.    
  18.    
  19. 工找到:×××条记录,耗时×××秒   
  20.    
  21. %result%     
  22.    
  23. 这里搜索的结构全都是百度那里过来的哦!   
  24.    
  25.    
  26.    
复制代码
   
其中%title%和%result%是等待替换的字符,为了替换这些字符,我们再增加一个方法,   
reCreatePage():   
  1.    
  2. def   reCreatePage(self,resultStr):   
  3.         demoStr   =   urllib.urlopen(demoPage).read()     #   get   the   demo   page   string   
  4.         reTitle   =   re.compile( &quot;%title% &quot;)   
  5.         demoStr   =   reTitle.sub(self.key,demoStr)     #   re   set   the   page   title   
  6.         reResult   =   re.compile( &quot;%result% &quot;)   
  7.         demoStr   =   reResult.sub(resultStr,demoStr)     #   re   set   the   page   result   
  8.         return   demoStr   
复制代码
   
这样就可以把模板中的%title%和%result%替换成我们想要的标签了。   
代码:   
  1.    
  2. #   the   main   programme   
  3. #   creator:   Singo   
  4. #   date:   2007-8-24   
  5. import   urllib   
  6. import   re   
  7. class   SearchThief:   
  8.         &quot; &quot; &quot;the   google   thief &quot; &quot; &quot;   
  9.         global   path,targetURL,demoPage   
  10.         path   =   &quot;pages\\ &quot;   
  11.         #   targetURL   =   &quot;http://www.google.cn/search?complete=1&hl=zh-CN&q= &quot;   
  12.         targetURL   =   &quot;http://www.baidu.com/s?wd= &quot;   
  13.         demoPage   =   path+ &quot;__demo__.html &quot;   
  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.                 webStrList   =   webStr.read().split( &quot;\r\n &quot;)   
  23.                 line   =   webStrList.index( &quot;   &quot;)+2     #   get   the   line   from   &quot;   &quot;   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.                 reTitle   =   re.compile( &quot;%title% &quot;)   
  29.                 demoStr   =   reTitle.sub(self.key,demoStr)     #   re   set   the   page   title   
  30.                 reResult   =   re.compile( &quot;%result% &quot;)   
  31.                 demoStr   =   reResult.sub(resultStr,demoStr)     #   re   set   the   page   result   
  32.                 return   demoStr   
  33.         def   setPageToFile(self,webStr):   
  34.                 reSetStr   =   re.compile( &quot;\r &quot;)   
  35.                 self.key   =   reSetStr.sub( &quot; &quot;,self.key)     #   replace   the   string   &quot;\r &quot;   
  36.                 targetFile   =   file(path+self.key+ &quot;.html &quot;, &quot;w &quot;)     #   open   the   file   for   &quot;w &quot;rite   
  37.                 targetFile.write(webStr)   
  38.                 targetFile.close()   
  39.                 print   &quot;done &quot;   
  40. inputKey   =   raw_input( &quot;Enter   you   want   to   search   -->   &quot;)   
  41. obj   =   SearchThief(inputKey)   
  42. obj.getPage()   
复制代码
   
这样我们就可以得到一个自己定义的风格而含有百度搜索出来的结果的页面,这里只做了标题和结果及的替换,同样道理,我们还可以把“百度快照”替换掉,我们还可以重新生成翻页控件,这样一个搜索小偷就基本完成啦。   
问题:   
用Python向Google请求时,Google会返回一个不是我们希望得到的页面,上面的内容是提示无权访问,Google很聪明,这步已经被他们想到了,但百度没做这样的限制哦,于是成功截取百度的数据。同样道理,还可以尝试其他搜索引擎,比如yisou和soso。   
后话:   
做个自己的页面风格,盗取baidu的搜索结果,打造自己的品牌而利用别人的数据,甚至去掉baidu的广告加上自己的广告,这种做法实在是太不厚道了,哈哈哈。该程序只为学习python用,具体来说没什么意义。   
[down=attachments/month_0708/92007826123442.rar]点击下载源码[/down]   
By   Singo   
原文出处:http://www.03th.com/singo/singoblog

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-59766-1-1.html 上篇帖子: 使用python自带的xml.dom创建和解析xml 下篇帖子: python urllib模块学习笔记
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表