python用flask实现百度搜索框

使用是python3.7版本

安装flask库 pip install flask

打开百度网址https://www.baidu.com/

分析百度的输入框。按F12键查看代码元素

python用flask实现百度搜索框_第1张图片

输入框text的名称为‘wd’

https://www.baidu.com/s?wd=python&rsv_spt=1&rsv_iqid=0xc9849ea400022122&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&rqlang=cn&tn=baiduhome_pg&rsv_enter=1&rsv_dl=tb&oq=php&inputT=1423&rsv_t=f0b4Z%2BftveP0QCWr%2F3Al6iGZKNU0QERdrleJaGNuHaXEhm9ru1LQQx2ZMXsHoa0v4Tp2&rsv_sug3=10&rsv_sug1=9&rsv_sug7=100&rsv_pq=a33b15990001df37&rsv_sug2=0&rsv_sug4=2194

python用flask实现百度搜索框_第2张图片

新建spider.py文件

# -*- coding:utf-8 -*-
import requests

def get_html(keywords):
	header = {
		"User-Agent":"Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1"
	}
	response = requests.get("https://www.baidu.com/s?wd={}&rsv_spt=1&rsv_iqid=0x9f603f1b000665af&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_sug3=9&rsv_sug1=8&rsv_sug7=100&rsv_sug2=0&inputT=3638&rsv_sug4=3638&rsv_sug=2".format(keywords),headers=header).text
	return response

 

新建app.py文件

代码如下:

# -*- coding:utf-8 -*-
from flask import Flask
from flask import render_template
from flask import request
from spider import get_html

app = Flask(__name__)
@app.route("/")
def index():
	return "hello world"
@app.route("/s")
def search():
	return render_template('search.html')

@app.route('/sp')
def baiduwb():
	key = request.args.get('wd')
	return get_html(key)

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

新建templates文件夹新建search.html文件




	
	Document


	

实现用命令  python app.py

在浏览器中输入 http://localhost/s 

 

谢谢,ALL!

 

 

你可能感兴趣的:(python)