1.运行环境

  1. python3
  2. centos7

2.Bottle的使用

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

bottle源码分析

bottle使用教程

3.代码

#app.py
from bottle import route, run ,Bottle ,error ,static_file ,request
from bottle import view , template from app import search
from data import db
import json #import views #实例化一个app
app = Bottle()
#读取api数据
data = db.data()
data.read() @app.route('/')
@view("home")
def index():
"""
首页
"""
return #template('home') @app.route('/<query>')
@view("search")
def query(query):
"""
执行查询操作:/关键词
"""
global data
key = data.touch()
res = search.query(query,key)
res = json.loads(res)
return res @app.route('/api/<query>')
def apiQuery(query):
"""
查询接口的api,返回json数据
"""
global data
key = data.touch()
res = search.apiQuery(query,key)
#res = json.loads(res)
return res @app.route('/man/getall')
def manGetall():
"""
获取所有api信息
"""
global data
#res = json.loads(res)
list = data.getAll()
res = ''
for i in list:
res = res + i[0] + " " + i[1] + "<br>"
print(res)
return res @app.route('/search')
@view("search")
def query2():
"""
另一种搜索传参方式:q=关键词
"""
global data
key = data.touch()
q = request.query.q
#print(q)
res = search.query(q,key)
res = json.loads(res)
return res @error(404)
def error404(error):
"""
404
"""
return 'Nothing here, sorry' @app.route('/static/<filename>')
def server_static(filename):
"""
静态文件返回数据
"""
#print("***")
#filename = filename + ".ico"
return static_file(filename, root='./static') app.run(host='localhost', port=9090)
#search.py
import urllib
import urllib.request
from urllib import parse
import json #指定站点搜索:siteSearch def query(q,key):
url = 'https://www.googleapis.com/customsearch/v1?cx=012564558536199079522:s1cewzl004i&safe=active&key='
q = parse.quote(q)
url = url + key + "&q=" + q
resB = urllib.request.urlopen(url)
res = resB.read().decode('utf-8')
return res def apiQuery(q,key):
url = 'https://www.googleapis.com/customsearch/v1?cx=012564558536199079522:s1cewzl004i&safe=active&key='
q = parse.quote(q)
url = url + key + "&q=" + q
resB = urllib.request.urlopen(url)
res = resB.read().decode('utf-8')
return res
#db.py
class data: def read(self):
fp = open("data//data.txt",encoding="utf-8")
'''
#方法一
line = fp.readline()
print(line)
key = []
i = 0 while(line):
str = line.split()
#print("***")
#print(str)
key.append(str)
line = fp.readline()
i = i + 1 self.key = key
fp.close()'''
#方法二
key = []
str = fp.read()
tmp = str.split("\n")
for i in range(len(tmp)):
key.append(tmp[i].split())
self.key = key
fp.close()
#print(key) def write(self):
"""
保存api key
"""
key = self.key
fp = open("data//data.txt","w+")
for i in range(len(key)):
fp.writelines(key[i][0]+" "+key[i][1])
fp.close() def touch(self):
"""
随机获取一个api key
"""
key = self.key
k = 0
for i in range(len(key)):
if(key[k][1]>key[i][1]):
k = i
x = key[k][1]
key[k][1] = (str)(1 + (int)(x))
#key[k][i] += 1
return key[k][0] def getAll(self):
"""
获取所有api key
"""
return self.key def add(self,list):
"""
添加api key
"""
self.key.append(list) def delOne(self,index):
"""
删除一个 api key
"""
self.key.pop(index)
#home.tpl
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<title>冒号--www.mho.cx</title>
<!-- 新 Bootstrap 核心 CSS 文件 -->
<link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script> <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="shortcut icon" href="static/fa.ico" />
</head>
<body> <div class="panel panel-default container navbar-static-top" style="max-width:900px;">
<div class="panel-body">
<h3 class="text-center"><a href="https://www.mho.cx">冒号搜索</a></h3>
<form class="bs-example bs-example-form container" style="max-width:800px;" role="form" method="get" action="https://mho.cx/search">
<div class="input-group"> <input type="text" class="form-control" placeholder="发现这个世界!" name="q" >
<span class="input-group-btn">
<button class="btn btn-default" type="summit">
GO!
</button>
</span> </div>
</form>
</div>
</div>
<div class="panel panel-default container" style="max-width:900px;">
<div class="panel-body">
<img src="http://img.zcool.cn/community/010b9c57748a930000012e7e419c08.png@2o.png" style="width:100%;">
</div>
</div> <div class="panel panel-default container navbar-static-bottom" style="max-width:900px;">
<div class="panel-body">
<h5 class="text-center"><a href="https://www.mho.cx">&copy;2019 冒号:</a></h3>
</div>
</div> </body>
</html>
#search.tpl
<!doctype html>
<html> <head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<title>冒号:搜索->
{{queries['request'][0]['searchTerms']}}
</title>
<!-- 新 Bootstrap 核心 CSS 文件 -->
<link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script> <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="shortcut icon" href="static/fa.ico" />
</head> <body> <div class="panel panel-default navbar-fixed-top">
<div class="panel-heading">
<h3 class="panel-title text-center"><a href="http://www.mho.cx">冒号搜索</a></h3>
</div>
<div class="panel-body">
<form class="bs-example bs-example-form container" role="form" style="max-width:800px;" method="get" action="https://mho.cx/search">
<div class="input-group">
<input type="text" class="form-control" placeholder="发现这个世界!" name="q" value="{{queries['request'][0]['searchTerms']}}">
<span class="input-group-btn">
<button class="btn btn-default" type="summit">
GO!
</button>
</span>
</div>
</form>
</div>
</div> <div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title text-center"><a href="https://www.mho.cx">冒号搜索</a></h3>
</div>
<div class="panel-body">
<form class="bs-example bs-example-form container" style="max-width:800px;" role="form" method="get" action="https://mho.cx/search">
<div class="input-group">
<input type="text" class="form-control" placeholder="发现这个世界!" name="q" value="{{queries['request'][0]['searchTerms']}}">
<span class="input-group-btn">
<button class="btn btn-default" type="button">
GO!
</button>
</span>
</div>
</form>
</div>
</div> %if queries['request'][0]['totalResults']!="0":
<div class="panel panel-default">
<div class="panel-body">
%for i in items:
<div class="well container" style="max-width:800px;">
<p>
<h4><a href="{{i['link']}}" target="_blank">{{i['title']}}</a></h4>
</p>
<p><strong>{{i['snippet']}}</strong></p>
<p>{{i['displayLink']}}</p>
</div>
%end
</div>
</div>
%else:
<div class="panel panel-default">
<div class="panel-body">
<div class="well container" style="max-width:800px;">
<p>
<h4>Sorry , no found</h4>
</p>
</div>
</div>
</div>
%end <div class="panel panel-default container navbar-static-bottom" style="max-width:800px;">
<div class="panel-body">
<h5 class="text-center"><a href="https://www.mho.cx">&copy;2019 冒号:</a></h3>
</div>
</div> </body> </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. [TCP/IP]ARP与RARP的总结

    一. 总述 简单的说,ARP协议就是将IP地址转换为MAC物理地址:而RARP,就是ARP的逆向,也就是将MAC物理地址转换为IP地址.看起来这两个协议是完全对称的,但发明这两个协议的初衷基本上没有什 ...

  2. Try .NET离线版

    https://github.com/dotnet/try Try .NET离线版 使用Try.NET创建可交互.NET文档   原文地址:Create Interactive .NET Docume ...

  3. net core (上)

    net core (上) 本文是基于Windows10的. 下载地址: https://code.visualstudio.com/ insider 版下载地址: https://code.visua ...

  4. (转)nginx域名访问的白名单配置梳理

    nginx域名访问的白名单配置梳理 原文:http://www.cnblogs.com/kevingrace/p/6086652.html 在日常运维工作中,会碰到这样的需求:设置网站访问只对某些ip ...

  5. OpenCV ——IplImage应用解析

    由于OpenCV主要针对的是计算机视觉方面的处理,因此在函数库中,最重要的结构体是IplImage结构.IplImage结构来源于Intel的另外一个函数库Intel Image Processing ...

  6. Kaggle八门神器(一):竞赛神器之XGBoost介绍

    Xgboost为一个十分有效的机器学习模型,在各种竞赛中均可以看到它的身影,同时Xgboost在工业届也有着广泛的应用,本文以Titanic数据集为研究对象,简单地探究Xgboost模型建模过程,同时 ...

  7. Java字节码分析

    目录 Java字节码分析 查看字节码详细内容 javap 实例分析 Java字节码分析 对于源码的效率,但从源码来看有时无法分析出准确的结果,因为不同的编译器版本可能会将相同的源码编译成不同的字节码, ...

  8. UI2_同步下载

    // // ViewController.m // UI2_同步下载 // // Created by zhangxueming on 15/7/17. // Copyright (c) 2015年 ...

  9. .net memcache

    非常感谢csdn及冷月宫主让我很快学会了.net操作 memcache 文章转自:http://download.csdn.net/detail/e_wsq/4358982 C#存取Memcache的 ...

  10. 邮箱/邮件地址的正则表达式及分析(JavaScript,email,regex)

    简言 在做用户注册时,常会用到邮箱/邮件地址的正则表达式.本文列举了几种方案,大家可以根据自己的项目情况,选择最适合的方案. 方案1 (常用) 规则定义如下: 以大写字母[A-Z].小写字母[a-z] ...