首页 > 其他分享 >利用flask+lxml写简约project

利用flask+lxml写简约project

时间:2022-12-05 17:36:18浏览次数:40  
标签:index lxml flask text app project phone data id

1.手机归属地查询

效果图

import requests
from lxml import etree
from flask import Flask,render_template,request

app = Flask(__name__)  

def get_mobile(phone):
    url =f'https://www.ip138.com/mobile.asp?mobile={phone}&action=mobile'



    headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
    }


    resp =requests.get(url=url,headers=headers)

    # 设置中文显示
    resp.encoding='utf-8'

    # print(resp.content)
    # print(resp.text)

    e = etree.HTML(resp.text)

    # infos = e.xpath('//tr/td[2]/span/text()')
    desc = e.xpath('//tbody/tr/td[1]/text()')
    infos = e.xpath('//tr/td[2]/span/text() | //tr/td[2]/a[1]/text()')

    return infos

    # for x,y in zip(desc,infos):
    #
    #     print( f'{x}:{y}')



# a = int(input('请输入你要查的手机号码:按q可退出'))

# while True:
#     a = int(input('请输入你要查的手机号码:按9可退出查询:'))
#     if len(str(a)) < 11:
#         print('输入无效,请输入有效11位数字')
#     if a == 9:
#         break
#     else:
#         get_mobile(a)

@app.route('/index')
def index():
    return render_template('index.html')  #当前目录下新建一个template目录 编写一个index.html文件

@app.route('/search_phone') # 建立路由
def search_phone():

    # return 'hello'
    phone = request.args.get('phone')

    data = get_mobile(phone)
    return '<br>'.join(data)


app.run(debug=True)

index.html

<form action="search_phone" method="get">
    手机号:<input type="text" name="phone" id="">
    <input type="submit" value="查询">
</form>

2.简约 点赞 记录

效果图

from flask import Flask,render_template,request

app = Flask(__name__)

data = [
    {"id":0,"name":"中秋节","num":0},
    {"id":1,"name":"春节","num":0},
    {"id":2,"name":"建军节","num":0},
]

@app.route('/index')
def findex():
    return render_template('index.html',data=data)

@app.route('/dianzan')
def dianzan():
    id = request.args.get('id')
    print(f'想要给{id}点赞!!!')

    data[int(id)]['num'] += 1
    return render_template('index.html',data=data)



app.run(port=5,debug=True)
<div>
        <h1>这是一个点赞系统</h1>
    </div>

    <table border="1">
        <tr>
        <td>id</td>
        <td>节假日名</td>
        <td>点赞数</td>
        <td>操作</td>
        </tr>
        {% for i in data %}
        <tr>
            <td>{{i.id}}</td>
            <td>{{i.name}}</td>
            <td>{{i.num}}</td>
            <td><a href="/dianzan?id={{ i.id }}">点赞</a></td>
        </tr>
        {% endfor %}
    </table>

标签:index,lxml,flask,text,app,project,phone,data,id
From: https://www.cnblogs.com/socoo-/p/16952922.html

相关文章