环境:
Ubuntu16.04 + python3.5.2 + Django1.11.8
功能点:
1、将 "常用单词.txt" 的翻译存入数据库中
2、实现用户的注册、登录验证
3、查询单词
1、若数据库中存在,则返回数据库中的信息
2、若数据库中不存在,则爬取有道中的翻译并返回,同时提示是否保存到本地
1、是:保存到本地数据库
2、否:返回到查询页继续查询
步骤:
1、百度到满足要求的 “常用单词.txt”,将其中的数据根据正则匹配进行分隔,写入mysql数据库中
2、利用Django构建服务器框架
3、编写爬虫模块,并导入Django框架中使用
Django文档结构
├── dictionary
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── __pycache__
│ │ ├── __init__.cpython-35.pyc
│ │ ├── settings.cpython-35.pyc
│ │ ├── urls.cpython-35.pyc
│ │ └── wsgi.cpython-35.pyc
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── index
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── 0002_users_is_active.py
│ │ ├── 0003_auto_20180424_1526.py
│ │ ├── __init__.py
│ │ └── __pycache__
│ │ ├── 0001_initial.cpython-35.pyc
│ │ ├── 0002_users_is_active.cpython-35.pyc
│ │ ├── 0003_auto_20180424_1526.cpython-35.pyc
│ │ └── __init__.cpython-35.pyc
│ ├── models.py
│ ├── __pycache__
│ │ ├── admin.cpython-35.pyc
│ │ ├── __init__.cpython-35.pyc
│ │ ├── models.cpython-35.pyc
│ │ ├── urls.cpython-35.pyc
│ │ ├── views.cpython-35.pyc
│ │ └── youdao.cpython-35.pyc
│ ├── static
│ ├── templates
│ │ ├── after_insert.html
│ │ ├── index.html
│ │ ├── login.html
│ │ ├── main2.html
│ │ ├── main.html
│ │ └── register.html
│ ├── tests.py
│ ├── urls.py
│ ├── views.py
│ └── youdao.py
├── manage.py
代码部分
dictionary/urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
urlpatterns += [
url(r'^', include('index.urls')),
]
dictionary/settings
"""
Django settings for dictionary project.
Generated by 'django-admin startproject' using Django 1.11.8.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'fccc1xu2tqde@(wzdn8=)5btxu3xi8lyrx=_f=gg_0jz8+6@m#'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib