爬虫 scrapy 抓取小说实例
2017-09-05 16:02
330 查看
以http://www.biquge.com/2_2970/ 这部小说为例,用scrapy对这部小说的章节进行抓取
保存后执行scrapy crawl xiaoshuo就可以看到在目录下的全部小说章节了
#coding=utf-8 import scrapy,os curpath = os.getcwd() noveldir = '' class novelSpider(scrapy.Spider): name = 'xiaoshuo' start_urls = ['http://www.biquge.com/2_2970/'] def __init__(self): self.noveldir = '' def parse(self,response): title = response.css('div#info h1::text').extract_first() #小说名 self.noveldir = os.path.join(curpath,title) self.log(self.noveldir ) if not os.path.exists(self.noveldir ): os.makedirs(self.noveldir ) #创建小说目录 self.log('开始下载%s' % title.encode('utf8')) for href in response.css('dd a').css('a::attr(href)'): #小说章节链接 yield response.follow(href,self.parse_page) def parse_page(self,response): filename = response.css('div.bookname h1::text').extract_first().strip() #章节名,同时作为文件名 self.log('开始下载 %s' % filename.encode('utf8')) with open(os.path.join(self.noveldir ,filename),'w+') as f: for item in response.css('div#content::text').extract(): #小说章节内容写到文件 f.write(item.encode("utf8")+"\n")
保存后执行scrapy crawl xiaoshuo就可以看到在目录下的全部小说章节了
相关文章推荐
- Scrapy 爬虫实例 抓取豆瓣小组信息并保存到mongodb中 推荐
- Scrapy爬虫框架抓取中文结果为Unicode编码,如何转换UTF-8编码的解决办法
- Scrapy爬虫框架教程(四)-- 抓取AJAX异步加载网页
- 搭建scrapy抓取javascript动态数据的爬虫
- 爬虫第五战 scrapy小说爬取
- python3+scrapy 趣头条爬虫实例
- scrapy爬虫之抓取《芳华》短评及词云展示
- scrapy google爬虫实例
- Scrapy爬虫入门系列3 将抓取到的数据存入数据库与验证数据有效性
- C# 爬虫 抓取小说
- Scrapy爬虫抓取网站数据
- python爬虫抓取晋江网一篇小说
- Python实例:网络爬虫抓取豆瓣3万本书(4)
- scrapy爬虫框架入门实例
- scrapy爬虫实例
- Python爬虫框架Scrapy实战之抓取户外数据
- C# 爬虫 抓取小说
- python爬虫框架scrapy实例详解
- 爬虫Scrapy学习指南之抓取新浪天气
- Python爬虫实战之使用Scrapy爬起点网的完本小说