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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

15行Python 仿百度搜索引擎

發布時間:2023/12/20 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 15行Python 仿百度搜索引擎 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

開發工具:PyCharm

開發環境:python3.6 + flask + requests

?

開發流程:

1. 啟動一個web服務

from flask import Flask app = Flask(__name__) if __name__ == '__main__':app.run(host='127.0.0.1', port=6666)

2. 增加app.route裝飾器

from flask import Flaskapp = Flask(__name__)@app.route('/') def index():return 'Hello World' if __name__ == '__main__':app.run(host='127.0.0.1', port=5000)

3. 增加index.html

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>仿百度搜索</title><style type="text/css">.align-center{position:fixed;left:30%;top:30%;margin-left:width/2;margin-top:height/2;}</style> </head> <body><form action="/s" method="get"><div class="align-center"><input type="search" name="key"> <input type="submit" value="搜索"><br></div></form> </body> </html> index.html

4. 增加 render_template

from flask import Flask from flask import render_template app = Flask(__name__)@app.route('/') def index():return render_template('index.html') if __name__ == '__main__':app.run(host='127.0.0.1', port=5000)

5. 增加返回結果

@app.route('/s') def search():return 'Hello World'

6. spider.py

import requestsdef getBdMsg(keyword):headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"}res = requests.get('https://www.baidu.com/s?wd={}'.format(keyword), headers = headers).textreturn res

7. 獲取搜索框關鍵字,通過爬蟲程序搜索,獲得百度搜索結果

from flask import Flask from flask import render_template from flask import request from spider import getBdMsg app = Flask(__name__)@app.route('/') def index():return render_template('index.html')@app.route('/s') def search():keyword = request.args.get("key")text = getBdMsg(keyword)return textif __name__ == '__main__':app.run(host='127.0.0.1', port=5000)

8. 修改spider.py的返回結果,通過鏈式replace(),替換百度圖標和“百度一下”

return res.replace('//www.baidu.com/img/baidu_jgylogo3.gif','static/images/google.png').replace('百度一下', 'Google')

?

?

附完整源碼:

# -*- coding: utf-8 -*- # @Time : 2018/3/19 12:46 # @Author : TanRong # @Software: PyCharm # @File : search.pyfrom flask import Flask from flask import render_template from spider import getBdMsg from flask import request# Flask(__name__).run() app = Flask(__name__)#app.route裝飾器 @app.route('/') def index():return render_template('index.html')@app.route('/s') def search():keyword = request.args.get('key')text = getBdMsg(keyword)return textif __name__ == '__main__':app.run() search.py # -*- coding: utf-8 -*- # @Time : 2018/3/21 18:07 # @Author : TanRong # @Software: PyCharm # @File : spider.pyimport requestsdef getBdMsg(keyword):# 必須加上請求頭,這樣才是瀏覽器請求,不然無返回結果# F12 - NetWork - Requeset Headers - UserAgentheaders = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"}res = requests.get('https://www.baidu.com/s?wd={}'.format(keyword), headers = headers).textreturn res.replace('//www.baidu.com/img/baidu_jgylogo3.gif','static/images/google.png').replace('百度一下','Google').replace('百度','Google') #鏈式replace()if __name__ == '__main__':getBdMsg('風景') spider.py <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>仿百度搜索</title><style type="text/css">.align-center{position:fixed;left:30%;top:30%;margin-left:width/2;margin-top:height/2;}</style> </head> <body><form action="/s" method="get"><div class="align-center"><input type="search" name="key"> <input type="submit" value="搜索"><br></div></form> </body> </html> index.html

?

轉載于:https://www.cnblogs.com/tanrong/p/8641321.html

總結

以上是生活随笔為你收集整理的15行Python 仿百度搜索引擎的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。