python==3.8
Djiango==4.1.2
(数据来源抓取小说网站数据,需要的直接下载.sql文件)
网站的基本模板:
base.html
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<style>
body {
background-color: rgb(233, 250, 255);
}
</style>
<title>MY小说网</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="{% url 'homepage' %}">主页</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="{% url 'allNovels' %}">全部小说</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'xuanhuan' %}">玄幻小说</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'xiuzhen' %}">修真小说</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'doshi' %}">都市小说</a>
</li>
</ul>
<form name="form" class="form-inline my-2 my-lg-0" action="http://www.xbiquge.la/modules/article/waps.php" method="post">
<input type="text" class="form-control mr-sm-2" value="请填入书名和作者" name="searchkey" class="search" id="wd" baidusug="2" autocomplete="off">
<button id="sss" class="btn btn-outline-success my-2 my-sm-0" type="submit"> 搜 索</button>
</form>
</div>
</nav>
<br/>
{% block content %}
{% endblock %}
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.slim.min.js"
integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"
integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/js/bootstrap.min.js"
integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6"
crossorigin="anonymous"></script>
</body>
</html>
小说目录页:
index.html
{% extends 'base.html' %}
{% block content %}
<div class="one">
<div class="picture">
<img src="{{result.0.4}}" alt="">
</div>
<div class="maininfo">
<div class="info">
<h1>{{ result.0.1 }}</h1>
<p>{{ result.0.3 }}</p>
</div>
<div class="intro">
<p>{{ result.0.2 }}</p>
</div>
</div>
</div>
<div class="one">
<dt>《{{result.0.1}}》正文</dt>
{% for data in result1%}
<dd>
<a href="{% url 'indexNum' book_id data.1 %}" style="font-size: 12px;">{{data.0}}</a>
</dd>
{% endfor %}
</div>
<style>
dt {
background: none repeat scroll 0 0 #C3DFEA;
display: inline;
float: left;
font-size: 14px;
line-height: 28px;
overflow: hidden;
text-align: center;
vertical-align: middle;
width: 99%;
margin: 1px 5px;
padding: 5px 10px;
}
dd {
border-bottom: 1px dashed #CCC;
display: inline;
float: left;
height: 25px;
line-height: 200%;
margin: 0 0 5px 0;
overflow: hidden;
text-align: left;
text-indent: 10px;
vertical-align: middle;
width: 33%;
}
.maininfo h1 {
color: #555555;
/* font-family: "黑体"; */
font-size: 28px;
font-weight: 700;
overflow: hidden;
margin: auto;
padding: 1px;
}
p {
color: #555555;
font: 15px "宋体";
padding: 10px 0px 0px;
/*width: 350px;*/
height: 27px;
}
.one {
width: 987px;
margin: 100px auto;
/*background-color: pink;*/
border: 2px solid rgb(136, 198, 229);
overflow: hidden;
/* padding: 10px; */
}
.two {
width: 987px;
margin: 0 0 100px 0;
/*background-color: pink;*/
border: 2px solid rgb(136, 198, 229);
overflow: hidden;
/* padding: 10px; */
}
.picture {
margin: 12px;
padding: 12px;
background: rgb(225, 236, 237);
width: 130px;
height: 180px;
float: left;
}
img {
width: 100%;
height: 100%;
}
.maininfo {
width: 800px;
/* height: 236px; */
/*background: wheat;*/
float: left;
}
.info {
width: 780px;
height: 54px;
/*background-color: skyblue;*/
margin: 10px;
/* padding: 10px; */
}
.intro {
width: 780px;
height: 538px;
margin: 20px 0px;
padding: 10px;
/*background: whitesmoke;*/
border-top: 1px dashed rgb(166, 198, 235);
}
.intro p {
text-indent: 1em;
/*margin: 5px auto;*/
margin-top: 10px;
/*padding: 10px;*/
line-height: 35px;
}
</style>
{% endblock %}
小说详情页:
indexNum.html
{% extends 'base.html' %}
{% block content %}
<div class="one">
<div class="book_name">
<h1>{{chapter_name.0.0}}</h1>
<div class="bottom1">
<a href="{% url 'indexNum' book_id id1 %}">上一章</a>
<a href="{% url 'index' book_id %}">章节目录</a>
<a href="{% url 'indexNum' book_id id2 %}">下一章</a>
</div>
</div>
</div>
<div class="one">
<dt>《正文》</dt>
<div class="content">
{%for data in content.0%}
{{data}}
{%endfor%}
</div>
</div>
<div class="bottom1">
<a href="{% url 'indexNum' book_id id1 %}">上一章</a>
<a href="{% url 'index' book_id %}">章节目录</a>
<a href="{% url 'indexNum' book_id id2 %}">下一章</a>
</div>
<style>
.content {
font-size: 19pt;
letter-spacing: 0.2em;
line-height: 150%;
padding-top: 15px;
width: 85%;
margin: auto;
}
.bottom1 {
width: 100%;
height: 30px;
margin: 5px;
text-align: center;
clear: both !important;
}
.bottom1 a {
color: rgb(8, 83, 8);
font-size: 14px;
margin-left: 10px;
margin-right: 10px;
text-decoration: none;
}
.book_name {
width: 100%;
height: 137px;
}
.book_name h1 {
font: 25px / 35px 黑体;
padding-top: 30px;
text-align: center;
}
dt {
background: none repeat scroll 0 0 #C3DFEA;
display: inline;
float: left;
font-size: 14px;
line-height: 28px;
overflow: hidden;
text-align: center;
vertical-align: middle;
width: 99%;
margin: 1px 5px;
padding: 5px 10px;
}
dd {
border-bottom: 1px dashed #CCC;
display: inline;
float: left;
height: 25px;
line-height: 200%;
margin: 0 0 5px 0;
overflow: hidden;
text-align: left;
text-indent: 10px;
vertical-align: middle;
width: 33%;
}
.maininfo h1 {
color: #555555;
/* font-family: "黑体"; */
font-size: 28px;
font-weight: 700;
overflow: hidden;
margin: auto;
padding: 1px;
}
p {
color: #555555;
font: 15px "宋体";
padding: 10px 0px 0px;
/*width: 350px;*/
height: 27px;
}
.one {
width: 987px;
margin: 35px auto;
/*background-color: pink;*/
border: 2px solid rgb(136, 198, 229);
overflow: hidden;
/* padding: 10px; */
}
.two {
width: 987px;
margin: 0 0 100px 0;
/*background-color: pink;*/
border: 2px solid rgb(136, 198, 229);
overflow: hidden;
/* padding: 10px; */
}
.picture {
margin: 12px;
padding: 12px;
background: rgb(225, 236, 237);
width: 130px;
height: 180px;
float: left;
}
img {
width: 100%;
height: 100%;
}
.maininfo {
width: 800px;
/* height: 236px; */
/*background: wheat;*/
float: left;
}
.info {
width: 780px;
height: 54px;
/*background-color: skyblue;*/
margin: 10px;
/* padding: 10px; */
}
.intro {
width: 780px;
height: 538px;
margin: 20px 0;
padding: 10px;
/*background: whitesmoke;*/
border-top: 1px dashed rgb(166, 198, 235);
}
.intro p {
text-indent: 1em;
/*margin: 5px auto;*/
margin-top: 10px;
/*padding: 10px;*/
line-height: 35px;
}
</style>
{{result}} {% endblock %}
数据读取显示部分:
views.py
from django.shortcuts import render
import requests
import pymysql
from django.views import View
from django.http import HttpResponse
class Homepage(View):
# 返回图片 玄幻小说 修真小说 都市小说
def get(self, request):
# 得到图片内容
# 接入本地数据库 host为本地名称 可以为IP地址 默认是localhost user为账号 password为密码 port为端口 db为库 charset为编码器
db = pymysql.connect(host="localhost", user="数据库用户名", password="密码", port=3306, db='novel_db', charset="utf8")
# 获取操作
cursor = db.cursor()
sql = 'select book_picture from n_book'
cursor.execute(sql)
result_jianjie = cursor.fetchall()
list_picture = []
for value in result_jianjie:
list_picture.append(value[0])
# 得到玄幻小说 都市小说 修真小说
sql = 'select * from n_book_type'
cursor.execute(sql)
result_jianjie = cursor.fetchall()
# print(result_jianjie)
xuanhuan = []
dushi = []
xiuzhen = []
for value in result_jianjie:
if (value[2] == "玄幻"):
xuanhuan.append(value)
elif (value[2] == "都市"):
dushi.append(value)
else:
xiuzhen.append(value)
# print(list_picture)
# print(xuanhuan)
# print(dushi)
# print(xiuzhen)
type_name = ["xuanhuan", "xiuzhen", "dushi"]
return render(request, 'home.html', {'picture': list_picture, 'xuanhuan': xuanhuan, 'dushi': dushi, 'xiuzhen': xiuzhen, "type_name": type_name})
class type_name(View):
def get(self, request, type_name):
# 修真
if(type_name=="xiuzhen"):
return render(request, 'allNovels.html', {})
# 都市
elif(type_name=="dushi"):
return render(request, 'allNovels.html', {})
# 玄幻
else:
return render(request, 'allNovels.html', {})
# return render(request, 'home.html', {"type_name": type_name})
def allNovels(request):
# 接入本地数据库 host为本地名称 可以为IP地址 默认是localhost user为账号 password为密码 port为端口 db为库 charset为编码器
db = pymysql.connect(host="localhost", user="root", password="密码", port=3306, db='novel_db', charset="utf8")
# 获取操作
cursor = db.cursor()
sql = "select book_id,book_name,chapter_name,book_author_name, id FROM n_chapter as g,(SELECT a.book_id,a.book_name,a.book_author_name, b.Max_chapater from n_book as a,(SELECT chapter_book_id, MAX(id) as Max_chapater FROM n_chapter GROUP BY `chapter_book_id`) as b where a.book_id=b.chapter_book_id) as f WHERE g.chapter_book_id=f.book_id and g.id=f.Max_chapater "
cursor.execute(sql)
result = cursor.fetchall()
# print(result)
type_name = "全部小说"
return render(request, 'allNovels.html', {"result": result, "type_name": type_name})
def xunahuan(request):
# 接入本地数据库 host为本地名称 可以为IP地址 默认是localhost user为账号 password为密码 port为端口 db为库 charset为编码器
db = pymysql.connect(host="localhost", user="root", password="密码", port=3306, db='novel_db', charset="utf8")
# 获取操作
cursor = db.cursor()
sql = 'select p.book_id,p.book_name,chapter_name,book_author_name,id from n_book_type as p, (select book_id,book_name,chapter_name,book_author_name, id FROM n_chapter as g,(SELECT a.book_id,a.book_name,a.book_author_name, b.Max_chapater from n_book as a,(SELECT chapter_book_id, MAX(id) as Max_chapater FROM n_chapter GROUP BY `chapter_book_id`) as b where a.book_id=b.chapter_book_id) as f WHERE g.chapter_book_id=f.book_id and g.id=f.Max_chapater) as o where o.book_id=p.book_id and p.book_type="玄幻"'
cursor.execute(sql)
result = cursor.fetchall()
# print(result)
type_name = "玄幻小说"
return render(request, 'allNovels.html', {"result": result, "type_name": type_name})
def dushi(request):
# 接入本地数据库 host为本地名称 可以为IP地址 默认是localhost user为账号 password为密码 port为端口 db为库 charset为编码器
db = pymysql.connect(host="localhost", user="root", password="密码", port=3306, db='novel_db', charset="utf8")
# 获取操作
cursor = db.cursor()
sql = 'select p.book_id,p.book_name,chapter_name,book_author_name,id from n_book_type as p, (select book_id,book_name,chapter_name,book_author_name, id FROM n_chapter as g,(SELECT a.book_id,a.book_name,a.book_author_name, b.Max_chapater from n_book as a,(SELECT chapter_book_id, MAX(id) as Max_chapater FROM n_chapter GROUP BY `chapter_book_id`) as b where a.book_id=b.chapter_book_id) as f WHERE g.chapter_book_id=f.book_id and g.id=f.Max_chapater) as o where o.book_id=p.book_id and p.book_type="都市"'
cursor.execute(sql)
result = cursor.fetchall()
# print(result)
type_name = "都市小说"
return render(request, 'allNovels.html', {"result": result, "type_name": type_name})
def xiuzhen(request):
# 接入本地数据库 host为本地名称 可以为IP地址 默认是localhost user为账号 password为密码 port为端口 db为库 charset为编码器
db = pymysql.connect(host="localhost", user="root", password="密码", port=3306, db='novel_db', charset="utf8")
# 获取操作
cursor = db.cursor()
sql = 'select p.book_id,p.book_name,chapter_name,book_author_name,id from n_book_type as p, (select book_id,book_name,chapter_name,book_author_name, id FROM n_chapter as g,(SELECT a.book_id,a.book_name,a.book_author_name, b.Max_chapater from n_book as a,(SELECT chapter_book_id, MAX(id) as Max_chapater FROM n_chapter GROUP BY `chapter_book_id`) as b where a.book_id=b.chapter_book_id) as f WHERE g.chapter_book_id=f.book_id and g.id=f.Max_chapater) as o where o.book_id=p.book_id and p.book_type="修真"'
cursor.execute(sql)
result = cursor.fetchall()
# print(result)
type_name = "修真小说"
return render(request, 'allNovels.html', {"result": result, "type_name": type_name})
class indexsHome(View):
def get(self, request, book_id):
# 取作品名字 作者名 小说简介 图片
# 取这本书的章节内容
# 接入本地数据库 host为本地名称 可以为IP地址 默认是localhost user为账号 password为密码 port为端口 db为库 charset为编码器
db = pymysql.connect(host="localhost", user="root", password="密码", port=3306, db='novel_db',charset="utf8")
# 获取操作
# 拿n_book
cursor = db.cursor()
sql = "select * from n_book where book_id =('%s')" % (book_id)
cursor.execute(sql)
result = cursor.fetchall()
# n_chapter
sql = "select chapter_name, id, chapter_book_id from n_chapter where chapter_book_id =('%s') order by id" % (book_id)
cursor.execute(sql)
result1 = cursor.fetchall()
return render(request, 'index.html', {"result": result, "result1": result1, "book_id": book_id})
class indexsHomeNum(View):
def get(self, request, book_id, id):
# # 取作品名字 作者名 小说简介 图片
# # 取这本书的章节内容
# # 接入本地数据库 host为本地名称 可以为IP地址 默认是localhost user为账号 password为密码 port为端口 db为库 charset为编码器
db = pymysql.connect(host="localhost", user="root", password="密码", port=3306, db='novel_db',charset="utf8")
# # 获取操作
# # 拿n_book
cursor = db.cursor()
sql = 'select chapter_content from n_chapter where id = ("%d") and chapter_book_id = ("%s")' % (id, book_id)
# sql = "select chapter_name, id, chapter_book_id from n_chapter where chapter_book_id =('%s')" % (id)
cursor.execute(sql)
result = cursor.fetchall()
sql = 'select chapter_name from n_chapter where id = ("%d") and chapter_book_id = ("%s")' % (id, book_id)
# sql = "select chapter_name, id, chapter_book_id from n_chapter where chapter_book_id =('%s')" % (id)
cursor.execute(sql)
chapter_name = cursor.fetchall()
# print(book_id, id)
# print(result)
return render(request, 'indexNum.html', {"content": result, "chapter_name": chapter_name, "book_id": book_id, "id2": id+1, "id1": id-1})
# return HttpResponse(book_id, id)
文件结构目录:
demo下载:
链接:https://pan.baidu.com/s/1jGT6Myo_jN2noS1FpRSlBA
提取码:jqs7
数据下载(users2.sql):
链接:https://pan.baidu.com/s/1zsPwPa_AheL3eq522yLdRw
提取码:f93q