码迷,mamicode.com
首页 > 编程语言 > 详细

Python 爬虫-抓取小说《鬼吹灯之精绝古城》

时间:2017-02-12 22:17:38      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:turn   鬼吹灯   docx   gui   目录   for   www   file   print   

想看小说《鬼吹灯之精绝古城》,可是网页版的好多广告,还要一页一页的翻,还无法复制,于是写了个小爬虫,保存到word里慢慢看。

代码如下:

技术分享
"""
爬取《鬼吹灯之精绝古城》小说
"""
from selenium import webdriver
import os
from docx import Document

class DownloadFiles():

    def __init__(self):
        self.baseUrl = http://www.luoxia.com/guichui/
        self.basePath = os.path.dirname(__file__)

    def makedir(self, name):
        path = os.path.join(self.basePath, name)
        isExist = os.path.exists(path)
        if not isExist:
            os.makedirs(path)
            print(File has been created.)
        else:
            print(The file is existed.)
        # 切换到该目录下
        os.chdir(path)

    def connect(self, url):
        try:
            driver = webdriver.PhantomJS()
            driver.get(url)
            print(url)
        except:
            "This page is not existed."
        return driver

    def getContent(self):
        doc = Document()
        self.makedir(storyFiles)
        for page in range(27426, 27461):
            print(The page number is :  + str(page))
            url = self.baseUrl + str(page) + .htm
            driver = self.connect(url)
            rList = driver.find_elements_by_xpath(//article/p)
            for r in rList:
                print(r.text)
                doc.add_paragraph(r.text)

        doc.save(guichuideng.doc)

if __name__ == __main__:
    obj = DownloadFiles()
    obj.getContent()
View Code

 

Python 爬虫-抓取小说《鬼吹灯之精绝古城》

标签:turn   鬼吹灯   docx   gui   目录   for   www   file   print   

原文地址:http://www.cnblogs.com/fredkeke/p/6391936.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!