preface
Some time ago, I made a small function to climb the girl’s set of pictures, which seemed to be very interesting to my friends. In order to learn Python, I specially formed a learning group to learn Python together. Ten Python crawlers, nine crawlers. However, Python can also do Web development, and I’ll show you how to make a novel site.
Related software
Software version function address Python3.7.1 scripting language visual development tool www.jetbrains.com/pych www.djangoproject.com/PyCharm2018.2.4 www.python.org/Django2.1.3Web framework arm/
Description of environment construction:
www.runoob.com/python3/pyt…
Crawl data
Do a novel website, content is a must, first of all we climb a novel “stars change” to the database.
Create a simple database table:
CREATE TABLE `novel` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Increment primary key',
`title` varchar(100) NOT NULL COMMENT 'title',
`content` text NOT NULL COMMENT 'content',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8Copy the code
Install the database driver and connection pool:
# database driver
pip install pymysql
Database connection pool
pip install DBUtilsCopy the code
Code implementation:
# -*- coding: UTF-8 -*-
# import requests library
import requests
# Import file operation library
import codecs
from bs4 import BeautifulSoup
import sys
import mysql_DBUtils
from mysql_DBUtils import MyPymysqlPool
import importlib
importlib.reload(sys)
Assign a request header to the request to mimic chrome
headers = {
'User-Agent': 'the 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'
# star change address
book = 'http://www.biquge.cm/2/2042/'
# define the DB
mysql = MyPymysqlPool("dbMysql")
Get chapter content
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")
# get the content of div tag id attribute content \xa0 is unbroken whitespace
content = texts[0].text.replace('\xa0' * 4, '\n')
return content
Write to database
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)
# main method
def main():
res = requests.get(book, headers=headers)
html = res.content
html_doc = str(html, 'gbk')
# Use HTML. Parser
soup = BeautifulSoup(html_doc, 'html.parser')
Get all chapters
a = soup.find('div', id='list').find_all('a')
print('Total chapters: %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()Copy the code
For more code see:
Gitee.com/52itstyle/P…
The Web implementation
Django is an open source Web application framework written in Python. MVC framework pattern is adopted, that is, model M, view V and controller C. It was originally developed as CMS (Content Management System) software to manage some of Lawrence publishing group’s news-focused websites.
The core components of the Django framework are:
-
Object relational maps for creating models
-
Perfect management interface designed for end users
-
First-class URL design
-
Designer-friendly template language
-
Caching system
Create a project
pip install Django
# Create a project
python django-admin.py startproject itstyle
# change directory
cd itstyle
# to create the App
python manage.py startapp novelCopy the code
Generally, a project has multiple apps, of course, the general app can also be used in multiple projects, and then start the service:
The default port is 8000
python manage.py runserverCopy the code
If you are prompted that the port is occupied, you can use another port:
python manage.py runserver 8001Copy the code
The project structure
The final code is as follows:
│ ├─ │ Settings. Py │ ├─ Novel │ Settings# Basic configuration│ │ urls. Py# URL mapping│ │ wsgi.py │ │ ├─templates# Related page│ will. HTML# section│ novel_list. HTML# First page of the novel├ ─ utils │ │ dbMysqlConfig CNFDatabase configuration parameters│ │ encoder. Py# code class│ │ mysql_DBUtils. PyDatabase connection pool└ ─ view │ index. Py# Background businessCopy the code
Note the main points
RESTful style
The controller urls. Py
from django.conf.urls import url
from django.urls import path
from view import index
urlpatterns = [
# Starchange
path(' ', index.main), # new
# chapter page regular match
path('chapter/<int:novel_id>/', index.chapter), # new
]Copy the code
Code implementation:
from django.http import HttpResponse
from django.shortcuts import render
from utils.mysql_DBUtils import mysql
# Starchange chapter list
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)
' ''a single chapter Here novel_id corresponding urls. Py of < int: novel_id > you can visit: 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)Copy the code
The list shows
Based on the data returned from the backend, it is displayed in the foreground. Here you can think of it as a Struts2 tag or JSTL tag in Java, which also stands for Vue:
{% for novel in novel_list %}
<a href="/chapter/{{novel.id}} "><li>{{ novel.title }}</li></a>
{% endfor %}Copy the code
summary
Add Teacher 2643804531 as a friend, she will send you the past information, timely send you the project oh
611481448, this is our group number, if you have any questions can consult in the group
At this point, a simple Web project prototype has been completed, of course, there is still a lot of optimization, friends can focus on learning Python from scratch, continue to update.
Source: gitee.com/52itstyle/P…