日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python discuz搜索api_Python + Bottle + 谷歌搜索Api 实现简单搜索引擎

發(fā)布時間:2024/10/14 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python discuz搜索api_Python + Bottle + 谷歌搜索Api 实现简单搜索引擎 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.運行環(huán)境

python3

centos7

2.Bottle的使用

使用bottle主要是因為它僅用python自帶的庫即可實現(xiàn)對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數(shù)據(jù)

data = db.data()

data.read()

@app.route('/')

@view("home")

def index():

"""

首頁

"""

return #template('home')

@app.route('/')

@view("search")

def query(query):

"""

執(zhí)行查詢操作:/關(guān)鍵詞

"""

global data

key = data.touch()

res = search.query(query,key)

res = json.loads(res)

return res

@app.route('/api/')

def apiQuery(query):

"""

查詢接口的api,返回json數(shù)據(jù)

"""

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

print(res)

return res

@app.route('/search')

@view("search")

def query2():

"""

另一種搜索傳參方式:q=關(guān)鍵詞

"""

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/')

def server_static(filename):

"""

靜態(tài)文件返回數(shù)據(jù)

"""

#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):

"""

隨機(jī)獲取一個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

冒號--www.mho.cx

冒號搜索

GO!

?2019 冒號:

#search.tpl

冒號:搜索->

{{queries['request'][0]['searchTerms']}}

冒號搜索

GO!

冒號搜索

GO!

%if queries['request'][0]['totalResults']!="0":

%for i in items:

{{i['title']}}

{{i['snippet']}}

{{i['displayLink']}}

%end

%else:

Sorry , no found

%end

?2019 冒號:

總結(jié)

以上是生活随笔為你收集整理的python discuz搜索api_Python + Bottle + 谷歌搜索Api 实现简单搜索引擎的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。