前端 用flask写一个搜索引擎

anxing7 · October 21, 2019 · 472 hits

用 flask 写一个搜索引擎 | stylem

var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "//hm.baidu.com/hm.js?57e94d016e201fba3603a8a2b0263af0"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })();

(function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = '<a href="https://zz.bdstatic.com/linksubmit/push.js" rel="nofollow" target="_blank">https://zz.bdstatic.com/linksubmit/push.js</a>';<br> } else { bp.src = '<a href="http://push.zhanzhang.baidu.com/push.js" rel="nofollow" target="_blank">http://push.zhanzhang.baidu.com/push.js</a>'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();

盒子

盒子

Posts List

写这个简陋的搜索引擎主要是能让我对 flask 框架有一定的认识,所以我把它记录下来

一个.py 主文件

# -*- coding: utf-8 -*-
from flask import Flask
from flask import render_template
from flask import request
import urllib.request
from flask_bootstrap import Bootstrap
app=Flask(__name__)
bootstrap=Bootstrap(app)
 #name为当前的主文件名

def getHtml(wd,pn):
    req=urllib.request.Request('https://www.baidu.com/s?wd=%s&pn=%s' %(wd,pn))
    req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0')
    return urllib.request.urlopen(req).read()

#装饰器
@app.route('/')#定义路由
def index():
    return render_template('querry.html')

@app.route('/s')
def search():
    if request.method=='GET':
        wd=request.args.get('wd')
        wd=urllib.request.quote(wd.encode('utf-8'))
        pn = request.args.get('pn')
        return getHtml(wd,pn)
    else:
        return None

if __name__=='__main__':
    app.debug=True
    app.run()

一个 template 文件夹下的.html 文件

<h1>一个搜索引擎的页面</h1>

<html>
<head>
    <meta charset="utf-8">
    <title>搜索界面</title>
</head>

<body bgcolor="#C0C0C0">
<form action="/s" method="get">
    <input type="text" class="txt" name="wd"  id="txt"/>
    <button type="button" class="btn btn-success">百度一下</button>
</body>
</html>

这个 html 文件就是展现出来的搜索页面,如果用到 css、bootstrap、js 等就会有一个吸引人的页面

重要的函数实现:

 # 搜索函数
@app.route('/s')
def search():
    if request.method=='GET':
        wd=request.args.get('wd') #因为你搜索的关键词都在wd参数中,所以用args解析得到的wd就是你搜索的词

        wd=urllib.request.quote(wd.encode('utf-8'))#编码成utf-8,输入搜索词汇可以输入中文

        pn = request.args.get('pn')#这里的pn参数控制搜索的页数,不加pn的话只能搜索到第一页内容

        return getHtml(wd,pn)
    else:
        return None



 # 获取访问页面     

        def getHtml(wd,pn):
            req=urllib.request.Request('https://www.baidu.com/s?wd=%s&pn=%s' %(wd,pn))#得到两个参数之后要把相应的url读取出来

            req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0')#模拟浏览器访问,所以增加一个header

            return urllib.request.urlopen(req).read()    

Close

var disqus_shortname = 'forsigner'; <p>var disqus_url = '<a href="http://yoursite.com/2017/09/21/flask%E5%86%99%E4%B8%80%E4%B8%AA%E6%90%9C%E7%B4%A2%E5%BC%95%E6%93%8E/" rel="nofollow" target="_blank">http://yoursite.com/2017/09/21/flask 写一个搜索引擎/</a>用';</p> <p>(function(){ var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })(); </p>

function loadScript(url, callback) { var script = document.createElement('script') script.type = 'text/javascript'; <p>if (script.readyState) { //IE script.onreadystatechange = function() { if (script.readyState == 'loaded' || script.readyState == 'complete') { script.onreadystatechange = null; callback(); } }; } else { //Others script.onload = function() { callback(); }; }</p> <p>script.src = url; document.getElementsByTagName('head')[0].appendChild(script); }</p> <p>window.onload = function() { loadScript('/js/bundle.js?235683', function() { // load success }); } </p>

No Reply at the moment.
You need to Sign in before reply, if you don't have an account, please Sign up first.