環(huán)境搭建說明:
http://xintu.runoob.com/python3/python3-install.html
爬取數(shù)據(jù)
做一個(gè)小說網(wǎng)站,內(nèi)容是必須的,首先我們爬取一本小說《星辰變》到數(shù)據(jù)庫。
創(chuàng)建一個(gè)簡(jiǎn)單的數(shù)據(jù)庫表:
CREATE TABLE `novel` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主鍵', `title` varchar(100) NOT NULL COMMENT '標(biāo)題', `content` text NOT NULL COMMENT '內(nèi)容', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8安裝數(shù)據(jù)庫驅(qū)動(dòng)以及連接池:
# 數(shù)據(jù)庫驅(qū)動(dòng)pip install pymysql# 數(shù)據(jù)庫連接池pip install DBUtils代碼實(shí)現(xiàn):
# -*- coding: UTF-8 -*-# 導(dǎo)入requests庫import requests# 導(dǎo)入文件操作庫import codecsfrom bs4 import BeautifulSoupimport sysimport mysql_DBUtilsfrom mysql_DBUtils import MyPymysqlPoolimport importlibimportlib.reload(sys)# 給請(qǐng)求指定一個(gè)請(qǐng)求頭來模擬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://xintu.biquge.cm'# 星辰變地址book = 'http://xintu.biquge.cm/2/2042/'# 定義DBmysql = MyPymysqlPool("dbMysql")# 獲取章節(jié)內(nèi)容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標(biāo)簽id屬性content的內(nèi)容 \xa0 是不間斷空白符 content = texts[0].text.replace('\xa0' * 4, '\n') return content# 寫入數(shù)據(jù)庫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') # 獲取所有的章節(jié) a = soup.find('div', id='list').find_all('a') print('總章節(jié)數(shù): %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()更多代碼詳見:
https://gitee.com/52itstyle/Python/tree/master/Day04
私信小編007即可獲取驚喜大禮包哦!
創(chuàng)建項(xiàng)目
# 安裝Web框架 pip install Django# 創(chuàng)建一個(gè)項(xiàng)目python django-admin.py startproject itstyle# 切換目錄cd itstyle# 創(chuàng)建Apppython manage.py startapp novel一般一個(gè)項(xiàng)目有多個(gè)app, 當(dāng)然通用的app也可以在多個(gè)項(xiàng)目中使用,然后啟動(dòng)服務(wù):
# 默認(rèn)端口是8000python manage.py runserver如果提示端口被占用,可以用其它端口:
python manage.py runserver 8001項(xiàng)目結(jié)構(gòu)
最終代碼,如下:
│ manage.py│ ├─novel│ │ settings.py # 基礎(chǔ)配置│ │ urls.py # URL映射│ │ wsgi.py│ │ __init__.py│ │ │ ├─templates # 相關(guān)頁面│ novel.html # 章節(jié)│ novel_list.html # 小說首頁│ ├─utils│ │ dbMysqlConfig.cnf # 數(shù)據(jù)庫配置參數(shù)│ │ encoder.py # 編碼類│ │ mysql_DBUtils.py # 數(shù)據(jù)庫連接池│ └─view │ index.py # 后臺(tái)業(yè)務(wù)要點(diǎn)備注
RESTful 風(fēng)格
控制器 urls.py
from django.conf.urls import urlfrom django.urls import pathfrom view import indexurlpatterns = [ # 《星辰變》首頁List path('', index.main), # new # 章節(jié)頁面 正則匹配 path('chapter/<int:novel_id>/', index.chapter), # new]代碼實(shí)現(xiàn)
from django.http import HttpResponsefrom django.shortcuts import renderimport utils.mysql_DBUtilsfrom utils.mysql_DBUtils import MyPymysqlPoolfrom utils.encoder import MyEncoderimport jsonmysql = MyPymysqlPool("dbMysql")# 《星辰變》章節(jié)列表def main(request): # 這里讀取10條,待優(yōu)化分頁 sql = "SELECT id,title FROM novel LIMIT 10;" result = mysql.getAll(sql) # 中文轉(zhuǎn)碼 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)'''單個(gè)章節(jié)訪問此處 novel_id 對(duì)應(yīng) 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) result['title'] = result['title'].decode('utf-8') result['content'] = result['content'].decode('utf-8') context = {'novel': result} return render(request, 'novel.html', context)列表展示
基于后端返回的數(shù)據(jù),在前臺(tái)進(jìn)行展示,這里你可以把它想象成Java中的Struts2標(biāo)簽或者JSTL標(biāo)簽,當(dāng)然也有點(diǎn)Vue的意思:
{% for novel in novel_list %} <a href="/chapter/{{novel.id}} "><li>{{ novel.title }}</li></a>{% endfor %}小結(jié)
至此,一個(gè)簡(jiǎn)單的Web項(xiàng)目雛形已經(jīng)完成,當(dāng)然還有很多需要優(yōu)化的地方,做成了項(xiàng)目,應(yīng)該就能有3w快到手了!路還長(zhǎng),還需努力!
掃描二維碼推送至手機(jī)訪問。
版權(quán)聲明:本文由信途科技轉(zhuǎn)載于網(wǎng)絡(luò),如有侵權(quán)聯(lián)系站長(zhǎng)刪除。
轉(zhuǎn)載請(qǐng)注明出處http://macbookprostickers.com/xintu/15792.html