Python学习练习项目,小白必看!

6 篇文章 0 订阅
1 篇文章 0 订阅

一.获取小说:

# 怎么发送请求
# pip install requests
import requests
# pip install lxml
from lxml import etree
# 发送给谁
url = 'https://www.85xs.cc/book/douluodalu1/1.html'
while True:
    # 伪装自己
    headers= {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36'
    }
    # 发送请求
    resp = requests.get(url,headers=headers)
    # 设置编码
    resp.encoding = 'utf-8'
    # 响应信息
    # print(resp.text)
    e = etree.HTML(resp.text)
    info = '\n'.join(e.xpath('//div[@class="m-post"]/p/text()'))
    title = e.xpath('//h1/text()')[0]
    url = f'https://www.85xs.cc{e.xpath("//tr/td[2]/a/@href")[0]}'
    # print(info)
    # print(title)
    # 保存
    with open('斗罗大陆.txt','w',encoding='utf-8') as f:
        f.write(title+'\n\n'+info+'\n\n')

    '''
    退出循环 break
    if url == '/book/douluodalu1/'
    '''

二.登录系统:

Python代码:

# pip install flask  Flask是为我们提供web服务的一个框架
from flask import Flask,render_template,request
app = Flask(__name__)
# 告诉浏览器应该显示什么
@app.route('/login')
def login():
    return render_template('login.html')
@app.route('/index')
def index():
    uname = request.args.get('uname')
    # 判断用户名是否正确
    return f"主页!!!欢迎登录{uname}"
app.run(debug=True)

网页代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <form action="/index" method="get">
        用户名:<input type="text" name="uname" id=""/>
        密码:<input type="password" name="pwd" />
        <input type="submit" value="登 录"/>
    </form>
</body>
</html>

三.抽奖系统:

Python代码:

# 让我们的电脑可以支持服务访问
# 需要一个web框架
# pip install Flask
from flask import Flask,render_template
from random import randint
app = Flask(__name__)

hero = [
'黑暗之女','狂战士','正义巨像','卡牌大师','德邦总管','无畏战车','诡术妖姬',
'猩红收割者','远古恐惧','正义天使','无极剑圣','牛头酋长','符文法师','亡灵战神','战争女神'
]

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

@app.route('/choujiang')
def choujiang():
    num = randint(0,len(hero)-1)
    return render_template('index.html',hero = hero, h = hero[num])

app.run(debug=True)

网页代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    {{ hero }}<br/>
    <a href="/choujiang">随机抽取</a><br/>
    您抽到了:{{ h }}
</body>
</html>

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值