一.获取小说:
# 怎么发送请求
# 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>