1-1. app
1-1-1. _ _ init _ _.py
import config
from flask import Flask
app = Flask( __name__)
app. config. from_object( config)
charset= utf8mb4'# SQLALCHEMY_DATABASE_URI = ' mysql+ pymysql: // crawler_user: Urun20181012@202.98 .248 .81 : 28817 / urun_postingmachine?
charset= utf8mb4'
SQLALCHEMY_TRACK_MODIFICATIONS = False
1-1-2. api.py
from . import app
from flask import jsonify
from flask import request
from flask import redirect, url_for
from flask import send_file
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy( )
with app. app_context( ) :
db. init_app( app)
错误时候
@app. errorhandler ( 404 )
def not_found ( error) :
一般使用
@app. route ( '/commentlist/get' , methods= [ 'GET' , 'POST' ] )
@app. route ( '/CommentList/get' , methods= [ 'GET' , 'POST' ] )
def get_comment_list ( ) :
url 后接方法名时
@app. route ( '/query/<query_name>' , methods= [ 'GET' , 'POST' ] )
def common_query ( query_name) :
query_obj = query_name. lower( )
if query_obj not in { 'account' , 'website' , 'board' , 'task' } :
request
用 法 作 用 redirect(url_for(‘get_api_doc’)) 跳转到,函数名为get_api_doc send_file(‘…/apidoc/api_all.html’) 返回…/apidoc/api_all.html,路径下的api_all.html request.args.to_dict() 请求url后的参数转化为字典形式(params形式) request.json (以json格式发送的)-(content-Type=application/json)-(raw) – 数据 jsonify(ret_data) 将ret_data,以json的格式返回 【return jsonify(ret_data)】 request.form.to_dict() 以表格的形式发送的 – 数据 request.method 请求的方法:【if request.method == ‘GET’:】 request.files.get(‘file’) 发送文件时,获取文件中键名为file
2-1映射类表
import config
from datetime import datetime
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy( )
def create_app ( ) :
app = Flask( __name__)
app. config. from_object( config)
with app. app_context( ) :
db. init_app( app)
return app
创建表
import config
from datetime import datetime
from flask import Flask
from extension import db
def create_app ( ) :
app = Flask( __name__)
app. config. from_object( config)
with app. app_context( ) :
db. init_app( app)
return app
class Account ( db. Model) :
"""
account表映射类
"""
__tablename__ = 'account'
id = db. Column( db. Integer, primary_key= True , autoincrement= True )
user_name = db. Column( db. String( 200 ) , nullable= False )
password = db. Column( db. String( 500 ) , nullable= False )
website_id = db. Column( db. Integer)
company_id = db. Column( db. Integer)
server_tag = db. Column( db. String( 200 ) )
state = db. Column( db. Integer, default= 1 )
status = db. Column( db. Text)
mobile = db. Column( db. String( 20 ) )
proxy = db. Column( db. String( 100 ) )
update_time = db. Column( db. DateTime, default= datetime. now, onupdate= datetime. now, nullable= False )
addon = db. Column( db. DateTime, default= datetime. now, nullable= False )
def __init__ ( self, user_name, password, website_id, company_id= 0 , server_tag= '' , state= 1 , status= '' , mobile= '' , proxy= None ) :
self. user_name = user_name
self. password = password
self. website_id = website_id
self. company_id = company_id
self. server_tag = server_tag
self. state = state
self. status = status
self. mobile = mobile
self. proxy = proxy
def __repr__ ( self) :
return '<%s => %s>' % ( self. __class__. __name__, self. user_name)
更新表
account_temp = Account( new_username, password, website_id, company_id, server_tag, mobile= mobile, proxy= proxy)
db. session. add( account_temp)
db. session. commit( )
查询表
account_temp = Account. query. filter_by( website_id= website_id, company_id= company_id) . all ( )
account_temp = Account. query. filter_by( ** filter_args) . first( )
account_obj = Account. query. filter_by( website_id= website_id)
》total = account_obj. count( )
account_obj = Account. query. get( account_id)
task_result_list = TaskResult. query. join( Task, TaskResult. task_id == Task. id ) \
. filter ( Task. process_type. in_( ( 2 , 5 ) ) , db. cast( TaskResult. update_time, db. DATE) <= db. cast( yesterday, db. DATE) , TaskResult. state == 0 ) . all ( )