小說的爬取(2)

爬取的是整個(gè)筆趣閣的多個(gè)類型下的小說毅臊,分四層進(jìn)行爬取理茎,并存儲(chǔ)在mysql數(shù)據(jù)庫(kù)中,為網(wǎng)站提供數(shù)據(jù)源

代碼如下:

import re
import urllib.request
# 數(shù)據(jù)庫(kù)的操作
import pymysql


class Sql(object):
    db = pymysql.connect(host="localhost", port=3306, db="novel", user="root", password="root", charset="utf8")
    print('連接上了!')

    def addnovel(self, sort_id, sort_name, bookname, imgurl, description, status, author):
        cur = self.db.cursor()
        cur.execute(
            'insert into novel(booktype,sortname,name,imgurl,description,status,author) values("%s","%s","%s","%s","%s","%s","%s")' % (
                sort_id, sort_name, bookname, imgurl, description, status, author))
        lastrowid = cur.lastrowid
        cur.close()
        self.db.commit()
        return lastrowid

    def addchapter(self, lastrowid, chaptname, content):
        cur = self.db.cursor()
        cur.execute('insert into chapter(novelid,title,content)value("%s","%s","%s")' % (lastrowid, chaptname, content))
        cur.close()
        self.db.commit()
#
#
mysql = Sql()


def type():  # 獲取小說類型
    html = urllib.request.urlopen("https://www.duquanben.com/").read()
    html = html.decode('gbk').replace('\n', '').replace('\t', '').replace('\r', '')
    reg = r'fn-left(.*?)subnav'
    html = re.findall(reg, html)
    for i in html:
        html = re.findall(r'book(.*?)/0/1/">(.*?)</a>', i)
        for sort_id, sort_name in html:
            getList(sort_id, sort_name)


def getList(sort_id, sort_name):  # 獲取書的鏈接
    html = urllib.request.urlopen('https://www.duquanben.com/book%s/0/1/' % sort_id).read().decode('gbk')
    # print(html)
    reg = r'<li>.*?href="(.*?)" target=".*?">.*?</a></li>'
    urlList = re.findall(reg, html)
    for url in urlList:
        # print(urlList)
        Novel(url, sort_id, sort_name)


def Novel(url, sort_id, sort_name):
    html = urllib.request.urlopen(url).read().decode('gbk').replace('\n', '').replace('\t', '').replace('\r',
                                                                                                        '').replace(
        '<br />', '').replace('&nbsp;', '')
    # print(html)
    chapturl, bookname = re.findall(
        '投票推薦</span></a></span><span class="btopt"><a href="(.*?)" title="(.*?)" target="_blank"><span>開始閱讀</span></a></span>',
        html)[0]
    description = re.findall(r'內(nèi)容簡(jiǎn)介.*?intro.*?>(.*?)</div>', html)[0]
    imgurl = re.findall(r'<img src="(.*?)" alt=".*?', html)[0]
    status = re.findall(r'float:right">(.*?)</div>', html)[0]
    author = re.findall(r'作者:(.*?)   最新章節(jié)', html)[0]
    # print(chapturl, bookname, description, imgurl, status, author)
    lastrowid = mysql.addnovel(sort_id, sort_name,bookname, imgurl, description, status,author)
    lastrowid=1
    print(chapturl, bookname,status, author)
    print("*"*100)
    chaptList(chapturl, sort_id, sort_name, lastrowid,bookname)


def chaptList(chapturl,sort_id, sort_name, lastrowid, bookname):  # 獲取了章節(jié)的部分鏈接和章節(jié)的名字
    html = urllib.request.urlopen(chapturl).read().decode('gbk').replace('\n', '').replace('\t', '').replace('\r', '')
    # print(html)
    reg = r'mulu_list(.*?)show_index3'
    chapthtml = re.findall(reg, html)
    for chapt in chapthtml:
        chaptlist = re.findall(r'<li><a href="(.*?)">(.*?)</a></li>', chapt, re.S)
        # print(chaptlist)
        for url1, chaptname in chaptlist:
            # print(bookname+"中的"+chaptname +"爬取結(jié)束")
            chaptcontent(url1, chapturl, lastrowid, chaptname)

def chaptcontent(url1, chapturl, lastrowid, chaptname):
    url = chapturl + url1
    # print(url)
    html = urllib.request.urlopen(url).read().decode('gbk').replace('\n', '').replace('\t', '').replace('\r', '')
    reg = r'class="contentbox">(.*?)<div class="ad00">'
    content = re.findall(reg, html)[0].replace('<br />', '').replace('&nbsp;', '').replace('>', '').replace('<','').replace(
        '[..]', '').replace('-a', '').replace('/a ', '')
    # print(content)
    mysql.addchapter(lastrowid, chaptname, content)
type()

在mysql可視乎界面中新建了兩個(gè)表


R`_LQ8K_`{GD7~80ZMNF(UQ.png
CREATE TABLE `chapter` (
  `id` int(20) NOT NULL AUTO_INCREMENT,
  `novelid` varchar(50) DEFAULT NULL,
  `title` varchar(50) DEFAULT NULL,
  `content` longtext,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=103 DEFAULT CHARSET=utf8;


CREATE TABLE `novel` (
  `id` int(50) NOT NULL AUTO_INCREMENT,
  `booktype` varchar(20) DEFAULT NULL,
  `sortname` varchar(50) DEFAULT NULL,
  `name` varchar(50) DEFAULT NULL,
  `imgurl` varchar(50) DEFAULT NULL,
  `description` varchar(200) DEFAULT NULL,
  `status` varchar(50) DEFAULT NULL,
  `author` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;


學(xué)習(xí)網(wǎng)站:b站

爬取結(jié)果如下:
![~FN7FKIP0T]FTAZUJLOR58.png

![9E@55TKMW%G0[R40$_526.png](https://upload-images.jianshu.io/upload_images/11616627-2e189951152a475d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/540)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末管嬉,一起剝皮案震驚了整個(gè)濱河市皂林,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌蚯撩,老刑警劉巖础倍,帶你破解...
    沈念sama閱讀 216,470評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異胎挎,居然都是意外死亡沟启,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門犹菇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來德迹,“玉大人,你說我怎么就攤上這事揭芍「旄悖” “怎么了?”我有些...
    開封第一講書人閱讀 162,577評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵沼沈,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我币厕,道長(zhǎng)列另,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,176評(píng)論 1 292
  • 正文 為了忘掉前任旦装,我火速辦了婚禮页衙,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘阴绢。我一直安慰自己店乐,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,189評(píng)論 6 388
  • 文/花漫 我一把揭開白布呻袭。 她就那樣靜靜地躺著眨八,像睡著了一般。 火紅的嫁衣襯著肌膚如雪左电。 梳的紋絲不亂的頭發(fā)上廉侧,一...
    開封第一講書人閱讀 51,155評(píng)論 1 299
  • 那天页响,我揣著相機(jī)與錄音,去河邊找鬼段誊。 笑死闰蚕,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的连舍。 我是一名探鬼主播没陡,決...
    沈念sama閱讀 40,041評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼索赏!你這毒婦竟也來了盼玄?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,903評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤参滴,失蹤者是張志新(化名)和其女友劉穎强岸,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體砾赔,經(jīng)...
    沈念sama閱讀 45,319評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蝌箍,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,539評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了暴心。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片妓盲。...
    茶點(diǎn)故事閱讀 39,703評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖专普,靈堂內(nèi)的尸體忽然破棺而出悯衬,到底是詐尸還是另有隱情,我是刑警寧澤檀夹,帶...
    沈念sama閱讀 35,417評(píng)論 5 343
  • 正文 年R本政府宣布筋粗,位于F島的核電站,受9級(jí)特大地震影響炸渡,放射性物質(zhì)發(fā)生泄漏娜亿。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,013評(píng)論 3 325
  • 文/蒙蒙 一蚌堵、第九天 我趴在偏房一處隱蔽的房頂上張望买决。 院中可真熱鬧,春花似錦吼畏、人聲如沸督赤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)躲舌。三九已至,卻和暖如春性雄,著一層夾襖步出監(jiān)牢的瞬間孽糖,已是汗流浹背枯冈。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留办悟,地道東北人尘奏。 一個(gè)月前我還...
    沈念sama閱讀 47,711評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像病蛉,于是被迫代替她去往敵國(guó)和親炫加。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,601評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容

  • # 一铺然、框架概述 # 課程概述 1. laravel 4天(之前TP框架還是很大的區(qū)別)(國(guó)外框架) 2. 在線教...
    關(guān)進(jìn)一閱讀 371評(píng)論 0 0
  • # 信息系統(tǒng)前沿課--范老師授課 #第二次作業(yè)## 一、重新使用Markdown ##熟悉語(yǔ)法沽瘦,了解Markdow...
    thought_10c7閱讀 708評(píng)論 0 0
  • 【綜合】【學(xué)校教育】{辦公文檔}http://www.reibang.com/p/82aba897d1eb{辦公文...
    5a15944f1ba2閱讀 598評(píng)論 0 1
  • 【綜合】【賬號(hào)】13831953942-13930914646-13930914564【賬號(hào)】【串號(hào)驗(yàn)證】8699...
    5a15944f1ba2閱讀 489評(píng)論 0 1
  • 【綜合】【賬號(hào)】13831953942-13930914646-13930914564【賬號(hào)】【串號(hào)驗(yàn)證】8699...
    5a15944f1ba2閱讀 534評(píng)論 0 4