爬取豆瓣电影信息所使用的flask框架的知识。

本文介绍如何利用Flask框架构建一个简单的爬虫应用,用于抓取豆瓣电影的相关信息。通过阅读`app.py`,`index.html`,`register.html`和`result.html`,你可以了解到如何结合Python爬虫与Web开发,实现数据的展示和用户交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

app.py

from flask import Flask , render_template,request
import datetime
app = Flask(__name__)

# 路由解析,通过用户访问的路径,匹配相应的函数。“/”
# @app.route('/')
# def hello_world():
#     return 'Hello world!'

# debug模式的开启


@app.route('/index')
def hello():
    return "hello"

# 拿到指定名字 用尖角号
# 通过访问路径获取用户的字符串参数
@app.route('/user/<name>')
def welcom1(name):
    return "hello,%s"%name

# 通过访问路径获取用户的整型参数,前面的“int:”是规定的格式   此外还有 float类型 同int类型访问方式
@app.route('/user/<int:id>')
def welcom2(id):
    return "hello,%d号会员"%id

# 路由路径不能重复,用户通过唯一路径来访问特定的函数

# 返回给用户渲染后的网页文件
# @app.route("/")
# def index2():
#     return render_template("index.html")


#向页面传递变量
@app.route("/")
def index2():
    time = datetime.date.today()   #普通变量
    name = ['zhang','wang','zhao'] #列表类型
    task ={"任务":"打扫卫生","时间":"3小时"} #字典类型
    return render_template("index.html",var = time,list = name,task=task)


#表单提交
@app.route("/test/register")
def register():
    return render_template("register.html")


#接受表单提交的路由,需要指定methods为post
@app.route("/result",methods=['POST','GET'])
def result():
    # 获取表单中内容
    if request.method =="POST":
        result = request.form # 将字典放入如result中
        return render_template("result.html",result=result)


if __name__ == '__main__':
    app.run()

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    今天是{{ var }},欢迎光临。<br/>
    今天值班的有:<br/>
    {%  for data in list %}       <!-- 用大括号和百分号括起来是控制结构,还有if -->
        <li> {{ data }} </li>
    {%  endfor      %}

    任务:<br/>    <!--了解一下如何在页面打印表格,以及如何迭代-->
        <table border="1">
           {% for key,value in task.items()%}    #迭代器<!--[(key,value),(key,value),(key,value)]-->
            <tr>
                <td>{{ key }}</td>
                <td>{{ value }}</td>
            </tr>
            {% endfor %}
        </table>



</body>
</html>

register.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<form action = "{{ url_for('result') }}" method="POST" >
    <p>姓名:<input type="text",name="姓名"></p>
    <p>年龄:<input type="text",name="年龄"></p>
    <p>性别:<input type="text",name="性别"></p>
    <p>地址:<input type="text",name="地址"></p>
    <P><input type="submit" value="提交"></P>
</form>
</body>
</html>

result.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    result
   <table >
           {% for key,value in result.items() %}    #迭代器<!--[(key,value),(key,value),(key,value)]-->
            <tr>
                <th>{{ key }}</th>
                <td>{{ value }}</td>
            </tr>
            {% endfor %}
        </table>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值