15行用Python实现仿百度搜索引擎
生活随笔
收集整理的這篇文章主要介紹了
15行用Python实现仿百度搜索引擎
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
工具:
開發(fā)工具:PyCharm
開發(fā)環(huán)境:python3.6 + flask + requests
開發(fā)流程:
1. 啟動(dòng)一個(gè)web服務(wù)
from flask import Flask app = Flask(__name__) if __name__ == '__main__':app.run(host='127.0.0.1', port=6666)2. 增加app.route裝飾器
''' 遇到問題沒人解答?小編創(chuàng)建了一個(gè)Python學(xué)習(xí)交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯(cuò)的視頻學(xué)習(xí)教程和PDF電子書! ''' 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.html4. 增加 render_template
''' 遇到問題沒人解答?小編創(chuàng)建了一個(gè)Python學(xué)習(xí)交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯(cuò)的視頻學(xué)習(xí)教程和PDF電子書! ''' 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. 增加返回結(jié)果
@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 res7. 獲取搜索框關(guān)鍵字,通過爬蟲程序搜索,獲得百度搜索結(jié)果
''' 遇到問題沒人解答?小編創(chuàng)建了一個(gè)Python學(xué)習(xí)交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯(cuò)的視頻學(xué)習(xí)教程和PDF電子書! ''' 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的返回結(jié)果,通過鏈?zhǔn)絩eplace(),替換百度圖標(biāo)和“百度一下”
return res.replace('//www.baidu.com/img/baidu_jgylogo3.gif','static/images/google.png').replace('百度一下', 'Google')
附完整源碼:
search.py
spider.py
# -*- coding: utf-8 -*- ''' 遇到問題沒人解答?小編創(chuàng)建了一個(gè)Python學(xué)習(xí)交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯(cuò)的視頻學(xué)習(xí)教程和PDF電子書! ''' import requestsdef getBdMsg(keyword):# 必須加上請求頭,這樣才是瀏覽器請求,不然無返回結(jié)果# 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') #鏈?zhǔn)絩eplace()if __name__ == '__main__':getBdMsg('風(fēng)景')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>總結(jié)
以上是生活随笔為你收集整理的15行用Python实现仿百度搜索引擎的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对Python中路径操作指南
- 下一篇: Python:向函数传递任意数量的实参