1.运行环境

  1. python3
  2. centos7

2.Bottle的使用

使用bottle主要是因为它仅用python自带的库即可实现对web的搭建。

bottle源码分析

bottle使用教程

3.代码

  1. #app.py
  2. from bottle import route, run ,Bottle ,error ,static_file ,request
  3. from bottle import view , template
  4. from app import search
  5. from data import db
  6. import json
  7. #import views
  8. #实例化一个app
  9. app = Bottle()
  10. #读取api数据
  11. data = db.data()
  12. data.read()
  13. @app.route('/')
  14. @view("home")
  15. def index():
  16. """
  17. 首页
  18. """
  19. return #template('home')
  20. @app.route('/<query>')
  21. @view("search")
  22. def query(query):
  23. """
  24. 执行查询操作:/关键词
  25. """
  26. global data
  27. key = data.touch()
  28. res = search.query(query,key)
  29. res = json.loads(res)
  30. return res
  31. @app.route('/api/<query>')
  32. def apiQuery(query):
  33. """
  34. 查询接口的api,返回json数据
  35. """
  36. global data
  37. key = data.touch()
  38. res = search.apiQuery(query,key)
  39. #res = json.loads(res)
  40. return res
  41. @app.route('/man/getall')
  42. def manGetall():
  43. """
  44. 获取所有api信息
  45. """
  46. global data
  47. #res = json.loads(res)
  48. list = data.getAll()
  49. res = ''
  50. for i in list:
  51. res = res + i[0] + " " + i[1] + "<br>"
  52. print(res)
  53. return res
  54. @app.route('/search')
  55. @view("search")
  56. def query2():
  57. """
  58. 另一种搜索传参方式:q=关键词
  59. """
  60. global data
  61. key = data.touch()
  62. q = request.query.q
  63. #print(q)
  64. res = search.query(q,key)
  65. res = json.loads(res)
  66. return res
  67. @error(404)
  68. def error404(error):
  69. """
  70. 404
  71. """
  72. return 'Nothing here, sorry'
  73. @app.route('/static/<filename>')
  74. def server_static(filename):
  75. """
  76. 静态文件返回数据
  77. """
  78. #print("***")
  79. #filename = filename + ".ico"
  80. return static_file(filename, root='./static')
  81. app.run(host='localhost', port=9090)
  1. #search.py
  2. import urllib
  3. import urllib.request
  4. from urllib import parse
  5. import json
  6. #指定站点搜索:siteSearch
  7. def query(q,key):
  8. url = 'https://www.googleapis.com/customsearch/v1?cx=012564558536199079522:s1cewzl004i&safe=active&key='
  9. q = parse.quote(q)
  10. url = url + key + "&q=" + q
  11. resB = urllib.request.urlopen(url)
  12. res = resB.read().decode('utf-8')
  13. return res
  14. def apiQuery(q,key):
  15. url = 'https://www.googleapis.com/customsearch/v1?cx=012564558536199079522:s1cewzl004i&safe=active&key='
  16. q = parse.quote(q)
  17. url = url + key + "&q=" + q
  18. resB = urllib.request.urlopen(url)
  19. res = resB.read().decode('utf-8')
  20. return res
  1. #db.py
  2. class data:
  3. def read(self):
  4. fp = open("data//data.txt",encoding="utf-8")
  5. '''
  6. #方法一
  7. line = fp.readline()
  8. print(line)
  9. key = []
  10. i = 0
  11. while(line):
  12. str = line.split()
  13. #print("***")
  14. #print(str)
  15. key.append(str)
  16. line = fp.readline()
  17. i = i + 1
  18. self.key = key
  19. fp.close()'''
  20. #方法二
  21. key = []
  22. str = fp.read()
  23. tmp = str.split("\n")
  24. for i in range(len(tmp)):
  25. key.append(tmp[i].split())
  26. self.key = key
  27. fp.close()
  28. #print(key)
  29. def write(self):
  30. """
  31. 保存api key
  32. """
  33. key = self.key
  34. fp = open("data//data.txt","w+")
  35. for i in range(len(key)):
  36. fp.writelines(key[i][0]+" "+key[i][1])
  37. fp.close()
  38. def touch(self):
  39. """
  40. 随机获取一个api key
  41. """
  42. key = self.key
  43. k = 0
  44. for i in range(len(key)):
  45. if(key[k][1]>key[i][1]):
  46. k = i
  47. x = key[k][1]
  48. key[k][1] = (str)(1 + (int)(x))
  49. #key[k][i] += 1
  50. return key[k][0]
  51. def getAll(self):
  52. """
  53. 获取所有api key
  54. """
  55. return self.key
  56. def add(self,list):
  57. """
  58. 添加api key
  59. """
  60. self.key.append(list)
  61. def delOne(self,index):
  62. """
  63. 删除一个 api key
  64. """
  65. self.key.pop(index)
  1. #home.tpl
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <meta name="description" content="">
  8. <title>冒号--www.mho.cx</title>
  9. <!-- Bootstrap 核心 CSS 文件 -->
  10. <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  11. <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
  12. <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
  13. <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
  14. <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
  15. <link rel="shortcut icon" href="static/fa.ico" />
  16. </head>
  17. <body>
  18. <div class="panel panel-default container navbar-static-top" style="max-width:900px;">
  19. <div class="panel-body">
  20. <h3 class="text-center"><a href="https://www.mho.cx">冒号搜索</a></h3>
  21. <form class="bs-example bs-example-form container" style="max-width:800px;" role="form" method="get" action="https://mho.cx/search">
  22. <div class="input-group">
  23. <input type="text" class="form-control" placeholder="发现这个世界!" name="q" >
  24. <span class="input-group-btn">
  25. <button class="btn btn-default" type="summit">
  26. GO!
  27. </button>
  28. </span>
  29. </div>
  30. </form>
  31. </div>
  32. </div>
  33. <div class="panel panel-default container" style="max-width:900px;">
  34. <div class="panel-body">
  35. <img src="http://img.zcool.cn/community/010b9c57748a930000012e7e419c08.png@2o.png" style="width:100%;">
  36. </div>
  37. </div>
  38. <div class="panel panel-default container navbar-static-bottom" style="max-width:900px;">
  39. <div class="panel-body">
  40. <h5 class="text-center"><a href="https://www.mho.cx">&copy;2019 冒号:</a></h3>
  41. </div>
  42. </div>
  43. </body>
  44. </html>
  1. #search.tpl
  2. <!doctype html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <meta name="description" content="">
  8. <title>冒号:搜索->
  9. {{queries['request'][0]['searchTerms']}}
  10. </title>
  11. <!-- Bootstrap 核心 CSS 文件 -->
  12. <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  13. <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
  14. <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
  15. <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
  16. <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
  17. <link rel="shortcut icon" href="static/fa.ico" />
  18. </head>
  19. <body>
  20. <div class="panel panel-default navbar-fixed-top">
  21. <div class="panel-heading">
  22. <h3 class="panel-title text-center"><a href="http://www.mho.cx">冒号搜索</a></h3>
  23. </div>
  24. <div class="panel-body">
  25. <form class="bs-example bs-example-form container" role="form" style="max-width:800px;" method="get" action="https://mho.cx/search">
  26. <div class="input-group">
  27. <input type="text" class="form-control" placeholder="发现这个世界!" name="q" value="{{queries['request'][0]['searchTerms']}}">
  28. <span class="input-group-btn">
  29. <button class="btn btn-default" type="summit">
  30. GO!
  31. </button>
  32. </span>
  33. </div>
  34. </form>
  35. </div>
  36. </div>
  37. <div class="panel panel-default">
  38. <div class="panel-heading">
  39. <h3 class="panel-title text-center"><a href="https://www.mho.cx">冒号搜索</a></h3>
  40. </div>
  41. <div class="panel-body">
  42. <form class="bs-example bs-example-form container" style="max-width:800px;" role="form" method="get" action="https://mho.cx/search">
  43. <div class="input-group">
  44. <input type="text" class="form-control" placeholder="发现这个世界!" name="q" value="{{queries['request'][0]['searchTerms']}}">
  45. <span class="input-group-btn">
  46. <button class="btn btn-default" type="button">
  47. GO!
  48. </button>
  49. </span>
  50. </div>
  51. </form>
  52. </div>
  53. </div>
  54. %if queries['request'][0]['totalResults']!="0":
  55. <div class="panel panel-default">
  56. <div class="panel-body">
  57. %for i in items:
  58. <div class="well container" style="max-width:800px;">
  59. <p>
  60. <h4><a href="{{i['link']}}" target="_blank">{{i['title']}}</a></h4>
  61. </p>
  62. <p><strong>{{i['snippet']}}</strong></p>
  63. <p>{{i['displayLink']}}</p>
  64. </div>
  65. %end
  66. </div>
  67. </div>
  68. %else:
  69. <div class="panel panel-default">
  70. <div class="panel-body">
  71. <div class="well container" style="max-width:800px;">
  72. <p>
  73. <h4>Sorry , no found</h4>
  74. </p>
  75. </div>
  76. </div>
  77. </div>
  78. %end
  79. <div class="panel panel-default container navbar-static-bottom" style="max-width:800px;">
  80. <div class="panel-body">
  81. <h5 class="text-center"><a href="https://www.mho.cx">&copy;2019 冒号:</a></h3>
  82. </div>
  83. </div>
  84. </body>
  85. </html>

Python + Bottle + 谷歌搜索Api 实现简单搜索引擎的更多相关文章

  1. Golang 谷歌搜索api 实现搜索引擎(前端 bootstrap + jquery)

    Golang 谷歌搜索api 实现搜索引擎(前端 bootstrap + jquery) 体验 冒号搜索 1. 获取谷歌搜索api 谷歌搜索api教程 2. 后台调用 程序入口 main.go // ...

  2. python 调用图灵机器人api实现简单的人机交互

    接入流程例如以下,须要先注冊开发人员帐号,之后会得到一个32位的key,保存下来,用于以后发送数据.http://www.tuling123.com/ 请求方式 演示样例: # -*- coding: ...

  3. Lucene.net站内搜索—3、最简单搜索引擎代码

    目录 Lucene.net站内搜索—1.SEO优化 Lucene.net站内搜索—2.Lucene.Net简介和分词Lucene.net站内搜索—3.最简单搜索引擎代码Lucene.net站内搜索—4 ...

  4. python操作三大主流数据库(12)python操作redis的api框架redis-py简单使用

    python操作三大主流数据库(12)python操作redis的api框架redis-py简单使用 redispy安装安装及简单使用:https://github.com/andymccurdy/r ...

  5. FOFA爬虫大法——API的简单利用

    FOFA是一款网络空间搜索引擎,它通过进行网络空间测绘,帮助研究人员或者企业迅速进行网络资产匹配,例如进行漏洞影响范围分析.应用分布统计.应用流行度等. 何为API?如果你在百度百科上搜索,你会得到如 ...

  6. python重新利用shodan API

    前言: 之前写过一个shodan的API调用 感觉写的不这么好.然后现在重新写一个 shodan介绍: shodan是互联网上最可怕的搜索引擎. CNNMoney的一篇文章写道,虽然目前人们都认为谷歌 ...

  7. Python自动化开发 - RESTful API

    本节内容 1.  RESTful 简介 2.  RESTful 设计指南 3.  Django REST Framework 最佳实践 4.  理论拓展与开放平台 5.  API文档化与测试 一  R ...

  8. ASP.NET实现二维码 ASP.Net上传文件 SQL基础语法 C# 动态创建数据库三(MySQL) Net Core 实现谷歌翻译ApI 免费版 C#发布和调试WebService ajax调用WebService实现数据库操作 C# 实体类转json数据过滤掉字段为null的字段

    ASP.NET实现二维码 using System;using System.Collections.Generic;using System.Drawing;using System.Linq;us ...

  9. Python基础:一起来面向对象 (二) 之搜索引擎

    实例 搜索引擎 一个搜索引擎由搜索器.索引器.检索器和用户接口四个部分组成 搜索器就是爬虫(scrawler),爬出的内容送给索引器生成索引(Index)存储在内部数据库.用户通过用户接口发出询问(q ...

随机推荐

  1. $SCOJ4427 Miss Zhao's Graph$

    \(problem\) 给定一个包含n个顶点m条边的带权有向图,找一条边数最多的路径,且路径上的边的权值严格递增. 图中可能有重边和自环. \(题意非常简单:n个点 m个带权边 最多能连成多少条边\) ...

  2. vue路由的四种传值

    第一种:props 配置: 组件内定义: props: ['id'] 路由映射配置,开启props:true : { path: '/user/:id', component: User, props ...

  3. 【手撸一个ORM】第二步、封装实体描述和实体属性描述

    一.实体属性描述 [MyProperty.cs] Name,属性名称 PropertyInfo,反射获取的属性信息,后面很多地方需要通过该属性获取对应的实体类型,或调用SetValue进行赋值 Fie ...

  4. ORACLE行转列通用过程(转)

    1.使用视图 SQL code? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 create or r ...

  5. Hibernate 事物隔离级别

      Hibernate事务和并发控制                                            ++YONG原创,转载请注明 1.    事务介绍: 1.1.        ...

  6. java中循环的不同终止方式

    1.break:直接强行跳出当前循环,不再执行剩余代码.但在多重循环的情况下,若break在内层循环中,则仅仅终止了内层循环,外循环照常执行. 2.continue:仅仅终止此次循环. 3.retur ...

  7. 基于名称的虚拟主机-Apache

    基于名称的虚拟主机和基于IP的虚拟主机的对比 基于IP的虚拟主机使用连接的IP地址来识别(区分)正确的虚拟主机,所以对于每一个虚拟主机,你都需要有独立的IP地址. 基于名称的虚拟主机,服务器依赖于客户 ...

  8. ABAP自定义功能函数

    1.实现计算器中阶乘函数 FUNCTION zfun_mm_001. *"---------------------------------------------------------- ...

  9. God made relatives.Thank God we can choose our friends.

    God made relatives.Thank God we can choose our friends. 神决定了谁是你的亲戚, 幸运的是在选择朋友方面他给了你留了余地

  10. mysql主从设置windows

    MySQL 主从复制是其最重要的功能之一.主从复制是一台服务器充当主服务器,另一台或多台服务器充当从服务器,主机自动复制到从机.对于多级复制,数据服务器即可充当主机,也可充当从机.MySQL 复制的基 ...