CREATE TABLE `novel` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主键',
`title` varchar(100) NOT NULL COMMENT '标题',
`content` text NOT NULL COMMENT '内容',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8复制代码
# 数据库驱动
pip install pymysql
# 数据库链接池
pip install DBUtils复制代码
# -*- coding: UTF-8 -*-
# 导入requests库
import requests
# 导入文件操做库
import codecs
from bs4 import BeautifulSoup
import sys
import mysql_DBUtils
from mysql_DBUtils import MyPymysqlPool
import importlib
importlib.reload(sys)
# 给请求指定一个请求头来模拟chrome浏览器
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'}
server = 'http://www.biquge.cm'
# 星辰变地址
book = 'http://www.biquge.cm/2/2042/'
# 定义DB
mysql = MyPymysqlPool("dbMysql")
# 获取章节内容
def get_contents(chapter):
req = requests.get(url=chapter)
html = req.content
html_doc = str(html, 'gbk')
bf = BeautifulSoup(html_doc, 'html.parser')
texts = bf.find_all('div', id="content")
# 获取div标签id属性content的内容 \xa0 是不间断空白符
content = texts[0].text.replace('\xa0' * 4, '\n')
return content
# 写入数据库
def write_db(chapter, content):
sql = "INSERT INTO novel (title, content) VALUES(%(title)s, %(content)s);"
param = {"title": chapter, "content": content}
mysql.insert(sql, param)
# 主方法
def main():
res = requests.get(book, headers=headers)
html = res.content
html_doc = str(html, 'gbk')
# 使用自带的html.parser解析
soup = BeautifulSoup(html_doc, 'html.parser')
# 获取全部的章节
a = soup.find('div', id='list').find_all('a')
print('总章节数: %d ' % len(a))
for each in a:
try:
chapter = server + each.get('href')
content = get_contents(chapter)
chapter = each.string
write_db(chapter, content)
except Exception as e:
print(e)
mysql.dispose()
if __name__ == '__main__':
main()复制代码
pip install Django
# 建立一个项目
python django-admin.py startproject itstyle
# 切换目录
cd itstyle
# 建立App
python manage.py startapp novel复制代码
# 默认端口是8000
python manage.py runserver复制代码
python manage.py runserver 8001复制代码
│ manage.py
│
├─novel
│ │ settings.py # 基础配置
│ │ urls.py # URL映射
│ │ wsgi.py
│ │ __init__.py
│ │
│
├─templates # 相关页面
│ novel.html # 章节
│ novel_list.html # 小说首页
├─utils
│ │ dbMysqlConfig.cnf # 数据库配置参数
│ │ encoder.py # 编码类
│ │ mysql_DBUtils.py # 数据库链接池
└─view
│ index.py # 后台业务复制代码
from django.conf.urls import url
from django.urls import path
from view import index
urlpatterns = [
# 《星辰变》首页List
path('', index.main), # new
# 章节页面 正则匹配
path('chapter/<int:novel_id>/', index.chapter), # new
]复制代码
from django.http import HttpResponse
from django.shortcuts import render
from utils.mysql_DBUtils import mysql
# 《星辰变》章节列表
def main(request):
sql = "SELECT id,title FROM novel LIMIT 10;"
result = mysql.getAll(sql)
# result = json.dumps(result, cls=MyEncoder, ensure_ascii=False, indent=4)
# result = json.loads(result)
context = {'novel_list': result}
return render(request, 'novel_list.html', context)
# def chapter(request):
# id = request.GET['id']
# sql = "SELECT content FROM novel where id = %(id)s;"
# param = {"id": id}
# result = mysql.getOne(sql, param)
# context = {'novel': result}
# return render(request, 'novel.html', context)
''' 单个章节 此处 novel_id 对应 urls.py 中的 <int:novel_id> 你能够访问:http://localhost:8000/chapter/1/ '''
def chapter(request, novel_id):
sql = "SELECT title,content FROM novel where id = %(id)s;"
param = {"id": novel_id}
result = mysql.getOne(sql, param)
context = {'novel': result}
return render(request, 'novel.html', context)复制代码
{% for novel in novel_list %}
<a href="/chapter/{{novel.id}} "><li>{{ novel.title }}</li></a>
{% endfor %}复制代码
加2643804531乐乐老师为好友,她会发以往资料给您,及时发课题给您哦 html
611481448,这是咱们的群号,若是您有什么问题能够在群内咨询python