python 谁是小偷_python 小偷程序
分析了一些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方法得到整个页面的字符串,然后再解析这些字符串,完全有办法把中间结果部分抽取出来,抽出着串字符串,加上自己的头部和顶部和底部,那样搜索小偷的雏形就大概完成了,下面先写个测试代码。
[b]代码[/b]
[code]
# Search Thief
# creator: Singo
# date: 2007-8-24
import urllib
import re
class SearchThief:
" " "the google thief " " "
global path,targetURL
path = "pages\\ "
# targetURL = "http://www.google.cn/search?complete=1&hl=zh-CN&q= "
targetURL = "http://www.baidu.com/s?wd= "
def __init__(self,key):
self.key = key
def getPage(self):
webStr = urllib.urlopen(targetURL+self.key).read() # get the page string form the url
self.setPageToFile(webStr)
def setPageToFile(self,webStr):
reSetStr = 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]
这里只是要求用户输入一个关键字,然后向搜索引擎提交请求,把返回的页面保存到一个目录下,这只是一个测试的例子,如果要做真正的搜索小偷,完全可以不保存这个页面,把抽取出来的字符串加入到我们预先设计好的模板里面,直接以web的形式显示在客户端,那样就可以实现利用盗取某些搜索引擎的结果并构造新的页面呈现。
[b]继续:[/b]
看一下百度搜索结果页的源码,在搜索结构的那个table标签前面有个
[code]
def getResultStr(self,webStr):
webStrList = webStr.read().split( "\r\n ")
line = webStrList.index( "
resultStr = webStrList[line]
return resultStr
[/code]
既然得到结果列表,那么我们要把这个结果列表放到自己定义的页面里面,我们可以说这个页面叫模板:
[code]
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
SuperSingo搜索-%title%
工找到:×××条记录,耗时×××秒
这里搜索的结构全都是百度那里过来的哦!
[/code]
其中%title%和%result%是等待替换的字符,为了替换这些字符,我们再增加一个方法,
[b]reCreatePage():[/b]
[code]
def reCreatePage(self,resultStr):
demoStr = urllib.urlopen(demoPage).read() # get the demo page string
reTitle = re.compile( "%title% ")
demoStr = reTitle.sub(self.key,demoStr) # re set the page title
reResult = re.compile( "%result% ")
demoStr = reResult.sub(resultStr,demoStr) # re set the page result
return demoStr
[/code]
这样就可以把模板中的%title%和%result%替换成我们想要的标签了。
[b]代码:[/b]
[code]
# the main programme
# creator: Singo
# date: 2007-8-24
import urllib
import re
class SearchThief:
" " "the google thief " " "
global path,targetURL,demoPage
path = "pages\\ "
# targetURL = "http://www.google.cn/search?complete=1&hl=zh-CN&q= "
targetURL = "http://www.baidu.com/s?wd= "
demoPage = path+ "__demo__.html "
def __init__(self,key):
self.key = key
def getPage(self):
webStr = urllib.urlopen(targetURL+self.key) # get the page string form the url
webStr = self.getResultStr(webStr) # get the result part
webStr = self.reCreatePage(webStr) # re create a new page
self.setPageToFile(webStr)
def getResultStr(self,webStr):
webStrList = webStr.read().split( "\r\n ")
line = webStrList.index( "
resultStr = webStrList[line]
return resultStr
def reCreatePage(self,resultStr):
demoStr = urllib.urlopen(demoPage).read() # get the demo page string
reTitle = re.compile( "%title% ")
demoStr = reTitle.sub(self.key,demoStr) # re set the page title
reResult = re.compile( "%result% ")
demoStr = reResult.sub(resultStr,demoStr) # re set the page result
return demoStr
def setPageToFile(self,webStr):
reSetStr = 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]
这样我们就可以得到一个自己定义的风格而含有百度搜索出来的结果的页面,这里只做了标题和结果及的替换,同样道理,我们还可以把“百度快照”替换掉,我们还可以重新生成翻页控件,这样一个搜索小偷就基本完成啦。
[b]问题:[/b]
用Python向Google请求时,Google会返回一个不是我们希望得到的页面,上面的内容是提示无权访问,Google很聪明,这步已经被他们想到了,但百度没做这样的限制哦,于是成功截取百度的数据。同样道理,还可以尝试其他搜索引擎,比如yisou和soso。
[b]后话:[/b]
做个自己的页面风格,盗取baidu的搜索结果,打造自己的品牌而利用别人的数据,甚至去掉baidu的广告加上自己的广告,这种做法实在是太不厚道了,哈哈哈。该程序只为学习python用,具体来说没什么意义。
[down=attachments/month_0708/92007826123442.rar]点击下载源码[/down]
By Singo
原文出处:http://www.03th.com/singo/singoblog
python 谁是小偷_python 小偷程序相关推荐
- python 进度条_Python小程序系列——动态进度条(1)
Python动态进度条I 开始我们的第一个Python程序. 显示一个动态进度条,在同一个位置显示从1%到100% 源代码附上来: import sys #有关Python运行环境的变量和函数impo ...
- python选课管理系统_Python开发程序:选课系统
程序名称: 选课系统 角色:学校.学员.课程.讲师 要求: 1. 创建北京.上海 2 所学校 2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海 ...
- 天天向上python题目答案_python语言程序设计基础 习题 天天向上的力量(math.pow)...
实例1: 一年365天,,以第一天的能力值为基数1.0,当好好学习时能力值相比前一天提高1%,没有学习时能力值相比前天要降低1%,每天努力和每天放任,一年下来的能力值相差多少? 解析: 如果每天都好好 ...
- python重复抽奖_python—抽奖程序
抽奖程序: 1.每次抽奖完成,会提示是否继续抽奖,只有输入"是"才继续抽奖 2.抽奖逻辑: (1)生产0-100的整数,构造成列表 (2)构造三个列表,分别是一等奖.二等奖.三等奖 ...
- python 自动记录时间_python记录程序运行时间的几种方法
1.获取当前时间的两种方法: import datetime,time now = time.strftime("%Y-%m-%d %H:%M:%S") print now now ...
- php小偷替换代码,收藏的一个php小偷的核心程序
收藏的一个php小偷的核心程序 2021-01-23 10:53:58591 ob_start(); ob_implicit_flush(0); functionCheckCanGzip(){ glo ...
- python语言程序设计慕课_中国大学MOOC(慕课)_Python语言程序设计基础_试题及答案...
中国大学MOOC(慕课)_Python语言程序设计基础_试题及答案 更多相关问题 智慧职教: 反应级数的绝对值不会超过3,可以是正整数或者负整数,不能是分数. 在铆接过程中,铆孔的质量直接影响铆接质量 ...
- python均分纸牌_Python实现比较扑克牌大小程序代码示例
是Udacity课程的第一个项目. 先从宏观把握一下思路,目的是做一个比较德州扑克大小的问题 首先,先抽象出一个处理的函数,它根据返回值的大小给出结果. 之后我们在定义如何比较两个或者多个手牌的大小, ...
- python开发图形小程序_python小程序图画 python开发微信小程序
你用python写过哪些好玩的微信小程序? 首先要明确一点,python是后台语言,不能直接用python来写微信小程序的. 微信小程序是用javascript和css来写的. python只是用来给 ...
最新文章
- KOA2路由koa-router实现类似express router的文件结构设计---KOA入门学习
- linux c 时间函数 time difftime 简介
- python使用xlrd读取xlsx文件_005:【Python读取本地Excel文件】使用xlrd模块来读取本地Excel文件...
- 文巾解题 1. 两数之和
- 机房冷热通道系统整体解决方案
- 成功解决AttributeError: module 'tensorflow' has no attribute 'scalar_summary'
- 三态门三个状态vhdl_人防门是什么?为什么会侵线导致重庆地铁事故
- highcharts 动态生成x轴和折线图
- SAP Spartacus里的routerLink测试
- XGBClassifier()特征选择
- java面试技术问题_11个JAVA面试中常见技术问题
- 时间管理,从洗碗开始
- 施一公:如何提高英文的科研写作能力
- 决策树和基于决策树的集成方法(DT,RF,GBDT,XGB)复习总结
- 关于nagios 邮件报警问题
- js上传图片转base64格式
- 对jeecg框架的认识
- js 根据公历日期 算出农历_利用Javascript获取当前日期的农历日期
- 华为 A800-9000 服务器 离线安装MindX DL
- uniapp音乐播放器