Python 爬虫-抓取小说《鬼吹灯之精绝古城》
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 爬虫-抓取小说《鬼吹灯之精绝古城》相关的知识,希望对你有一定的参考价值。
想看小说《鬼吹灯之精绝古城》,可是网页版的好多广告,还要一页一页的翻,还无法复制,于是写了个小爬虫,保存到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()
以上是关于Python 爬虫-抓取小说《鬼吹灯之精绝古城》的主要内容,如果未能解决你的问题,请参考以下文章
JAVA爬虫进阶之springboot+webmagic抓取顶点小说网站小说