爬取小說網站整站小說內容 -《狗嗨默示錄》-

來源:互聯網
上載者:User

標籤:localhost   password   return   exe   dex   col   res   def   網站   

# !/usr/bin/env python# -*- coding: utf-8 -*-import urllib.requestimport reimport MySQLdbimport socketdomain = ‘http://www.quanshuwang.com‘headers = {    ‘User-Agent‘:‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36‘}#擷取分類列表def getTypeList(type):    req = urllib.request.Request(‘http://www.quanshuwang.com/map/%s.html‘%type)    req.headers = headers #替換頭資訊    #req.add_header() #添加單個頭資訊    res = urllib.request.urlopen(req)  # 擷取源碼    html = res.read().decode(‘gbk‘)  # 解碼    reg = r‘<a href="(/book/.+?)" target="_blank">(.+?)</a>‘    reg = re.compile(reg)  # 編譯    return re.findall(reg,html)def getNovelList(href):    req = urllib.request.Request(domain + href)    req.headers = headers    res = urllib.request.urlopen(req)    html = res.read().decode(‘gbk‘)    reg = r‘<li><a href="(.+?)" title="(.+?)">(.+?)</a></li>‘    reg = re.compile(reg)    return re.findall(reg,html)def getNovelContent(url):    req = urllib.request.Request(domain + url)    req.headers = headers    res = urllib.request.urlopen(req)    html = res.read().decode(‘gbk‘,‘ignore‘)    reg = r‘style5\(\);</script>(.*?)<script type="text/javascript">style6\(\)‘    reg = re.compile(reg,re.S)    print(domain + url)    return re.findall(reg,html)[0]class Sql(object):    conn = MySQLdb.connect(host=‘localhost‘,port=x,user=‘x‘,password=‘x‘,db=‘novel‘,charset=‘utf8‘)    def addnovels(self,sort,novelname):        cur = self.conn.cursor() #遊標        cur.execute("insert into novel(sort,novelname) values(‘%s‘,‘%s‘)"%(sort,novelname))        lastrowid = cur.lastrowid        cur.close()        self.conn.commit()        return lastrowid    def addchapters(self,novelid,chaptername,content):        cur = self.conn.cursor()        cur.execute("insert into chapter(novelid,chaptername,content) values(%s,‘%s‘,‘%s‘)"%(novelid,chaptername,content))        cur.close()        self.conn.commit()mysql = Sql()if __name__ == ‘__main__‘:    for type in range(1,10):        if type == 1:            sort = "玄幻魔法"        elif type == 2:            sort = "武俠修真"        elif type == 3:            sort = "曆史軍事"        elif type == 4:            sort = "女頻言情"        elif type == 5:            sort = "偵探推理"        elif type == 6:            sort = "網路動漫"        elif type == 7:            sort = "科幻小說"        elif type == 8:            sort = "恐怖靈異"        elif type == 9:            sort = "美文同人"        else:            print("請求的小說類型有誤!!!")        for href,novelname in getTypeList(type):            lastrowid = mysql.addnovels(sort,novelname)            for url,title,title in getNovelList(href):                try:                    print("正在爬取------------%s 《%s》 %s"%(sort,novelname,title))                    content = getNovelContent(href.replace(‘index.html‘,url))                    mysql.addchapters(novelid=lastrowid,chaptername=title,content=content)                    socket.setdefaulttimeout(30)                except Exception as e:                    print("串連中斷,發生錯誤:%s !!!!"%e)

 

爬取小說網站整站小說內容 -《狗嗨默示錄》-

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.