flask
Posted Howhy Blogs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flask相关的知识,希望对你有一定的参考价值。
#-*- coding:utf-8 -*- from flask import Flask from flask import render_template from flask import request from flask import views from flask_sqlalchemy import SQLAlchemy import config import urllib2 db=SQLAlchemy() class UserInfo(db.Model): __table__name=‘userinfo‘ id=db.Column(db.Integer,primary_key=True,autoincrement=True) name=db.Column(db.String(30),unique=True,nullable=True) password=db.Column(db.String(80),nullable=True) app=Flask(__name__) @app.route(‘/‘,methods=[‘GET‘,‘POST‘]) def index(): if request.method==‘GET‘: kw=request.args.get(‘name‘,‘‘) ##/?name=‘werwr‘ return render_template(‘index.html‘) elif request.method==‘POST‘: return class Home(views.MethodView): def get(self): # userinfo_obj=UserInfo(name=‘howhy‘,password=‘werwrewr‘) # db.session.add(userinfo_obj) # db.session.commit() kw=request.args.get(‘name‘,‘‘) headers={‘User-Agent‘:‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36‘} req=urllib2.Request(‘https://www.baidu.com/s?wd=%s‘%kw,headers=headers) ret=urllib2.urlopen(req).read() ret=ret.replace(r‘<form id="form" name="f" action="/s" class="fm">‘,‘<form action="/home" method="get">‘) return ret def post(self): kw=request.form.get(‘name‘,‘‘)##get_data()获取post所有数据包 headers={‘User-Agent‘:‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36‘} req=urllib2.Request(‘https://www.baidu.com/s?wd=%s‘%kw,headers=headers) ret=urllib2.urlopen(req).read() return ret #return request.form.get(‘name‘,‘‘) app.add_url_rule( ‘/home‘,view_func=Home.as_view(‘home‘) ) app.config.from_object(config) db.init_app(app) if __name__==‘__main__‘: app.run(debug=True)
以上是关于flask的主要内容,如果未能解决你的问题,请参考以下文章