笔趣阁是很好爬的网站了,这里简单爬取了全部小说链接和每本的全部章节链接,还想爬取章节内容在biquge.py里在加一个爬取循环,在pipelines.py添加保存函数即可

1 创建一个scrapy项目:crapy startproject biqugetest

2 cd biqugetest

3 生成一个爬虫:scrapy genspider biquge xbiquge.la

4 提取数据:完善spider,使用xpath等方法

5 保存数据:pipeline中保存数据

biquge.py

-- coding: utf-8 --

import scrapy

自定义spider类,继承scrapy.spider

from scrapytest.items import BiqugeItem, BiqugeItem_detail

class BiqugeSpider(scrapy.Spider):

# 爬虫名字

name = 'biquge'

# 允许爬取的范围,防止爬虫爬到别的网站

allowed_domains = ['xbiquge.la']

# 开始爬取的url地址

start_urls = ['http://www.xbiquge.la/xiaoshuodaquan/']

# 数据提取的方法,接受下载中间件传过来的response
def parse(self, response):
# scrapy的response对象可以直接进行xpath
# names = response.xpath('//div[@class="novellist"]//a/text()')
# print("names:%s" % names) # 获取具体数据文本的方式如下
# 分组
li_list = response.xpath('//div[@class="novellist"]//a')
i = 0
for li in li_list:
# 创建一个数据字典
dict_data = BiqugeItem()
# 利用scrapy封装好的xpath选择器定位元素,并通过extract()或extract_first()来获取结果
dict_data['name'] = li.xpath('.//text()').extract_first() # 书名
dict_data['link'] = li.xpath('.//@href').extract_first() # 书链接
# print(dict_data)
yield dict_data
if i < 2: # 这里限制先爬取一本
yield scrapy.Request(dict_data['link'], callback=self.parse_detail)
i += 1 # 小说计数 def parse_detail(self, response):
# dict_data = response.meta['dict_data']
section_data = BiqugeItem_detail()
section_list = response.xpath('//*[@id="list"]/dl/dd/a')
i = 0
for section in section_list:
section_data['section_link'] = 'http://www.xbiquge.la/' + section.xpath('./@href').extract_first()
section_data['section_name'] = section.xpath('./text()').extract_first()
yield section_data i += 1 # 章节计数

==============================================

pipelines.py

import json

from scrapytest.items import BiqugeItem_detail, BiqugeItem

class ScrapytestPipeline(object):

# 爬虫文件中提取数据的方法每yield一次item,就会运行一次

# 该方法为固定名称函数

def process_item(self, item, spider):

if isinstance(item, BiqugeItem):

str_data = json.dumps(dict(item), ensure_ascii=False) + '\n'

self.file.write(str_data)

return item

# 爬虫开启 , 打开文件  并且只会执行一次
def open_spider(self, spider):
self.file = open('全部小说.csv', 'w') # 爬虫关闭, 关闭文件
def close_spider(self, spider):
self.file.close()

class BiqugeDetailPipeline(object):

def open_spider(self, spider):

self.file = open('小说章节.csv', 'w')

def process_item(self, item, spider):
if isinstance(item, BiqugeItem_detail):
str_data = json.dumps(dict(item), ensure_ascii=False) + '\n'
self.file.write(str_data)
return item def close_spider(self, spider):
self.file.close()

==============================================

items.py

import scrapy

设置 爬取的key field

class BiqugeItem(scrapy.Item):

# define the fields for your item here like:

name = scrapy.Field()
link = scrapy.Field()

class BiqugeItem_detail(scrapy.Item):

section_link = scrapy.Field()

section_name = scrapy.Field()

# 如果使用item, key 写错了会报错, 避免手误

# 如果dict , key写错了不会报错, 新增一个key

scrapy框架爬取笔趣阁的更多相关文章

  1. scrapy框架爬取笔趣阁完整版

    继续上一篇,这一次的爬取了小说内容 pipelines.py import csv class ScrapytestPipeline(object): # 爬虫文件中提取数据的方法每yield一次it ...

  2. Jsoup-基于Java实现网络爬虫-爬取笔趣阁小说

    注意!仅供学习交流使用,请勿用在歪门邪道的地方!技术只是工具!关键在于用途! 今天接触了一款有意思的框架,作用是网络爬虫,他可以像操作JS一样对网页内容进行提取 初体验Jsoup <!-- Ma ...

  3. bs4爬取笔趣阁小说

    参考链接:https://www.cnblogs.com/wt714/p/11963497.html 模块:requests,bs4,queue,sys,time 步骤:给出URL--> 访问U ...

  4. Python爬取笔趣阁小说,有趣又实用

    上班想摸鱼?为了摸鱼方便,今天自己写了个爬取笔阁小说的程序.好吧,其实就是找个目的学习python,分享一下. 1. 首先导入相关的模块 import os import requests from ...

  5. python应用:爬虫框架Scrapy系统学习第四篇——scrapy爬取笔趣阁小说

    使用cmd创建一个scrapy项目: scrapy startproject project_name (project_name 必须以字母开头,只能包含字母.数字以及下划线<undersco ...

  6. scrapycrawl 爬取笔趣阁小说

    前言 第一次发到博客上..不太会排版见谅 最近在看一些爬虫教学的视频,有感而发,大学的时候看盗版小说网站觉得很能赚钱,心想自己也要搞个,正好想爬点小说能不能试试做个网站(网站搭建啥的都不会...) 站 ...

  7. 爬虫入门实例:利用requests库爬取笔趣小说网

    w3cschool上的来练练手,爬取笔趣看小说http://www.biqukan.com/, 爬取<凡人修仙传仙界篇>的所有章节 1.利用requests访问目标网址,使用了get方法 ...

  8. 使用scrapy框架爬取自己的博文(2)

    之前写了一篇用scrapy框架爬取自己博文的博客,后来发现对于中文的处理一直有问题- - 显示的时候 [u'python\u4e0b\u722c\u67d0\u4e2a\u7f51\u9875\u76 ...

  9. Python使用Scrapy框架爬取数据存入CSV文件(Python爬虫实战4)

    1. Scrapy框架 Scrapy是python下实现爬虫功能的框架,能够将数据解析.数据处理.数据存储合为一体功能的爬虫框架. 2. Scrapy安装 1. 安装依赖包 yum install g ...

随机推荐

  1. mybatis3 step by step 快速上手

    作者:Panda Fang 出处:http://www.cnblogs.com/lonkiss/p/6895617.html 原创文章,转载请注明作者和出处,未经允许不可用于商业营利活动 官方网站 h ...

  2. Android应用开发基础之五:网络编程(二)

    HttpClient 发送get请求 创建一个客户端对象 HttpClient client = new DefaultHttpClient(); 创建一个get请求对象 HttpGet hg = n ...

  3. MySQL命令行导入导出sql文件

    linux下 一.导出数据库用mysqldump命令(注意mysql的安装路径,即此命令的路径):1.导出数据和表结构(常用):mysqldump -u用户名 -p密码 数据库名 > 数据库名. ...

  4. FileHelpers 用法 z

    用FileHelplers导出csv数据: [DelimitedRecord(",")] [IgnoreEmptyLines()] [ConditionalRecord(Recor ...

  5. Oracle transport tablespace

    本来没想过发布这个文章,只是周边有一朋友工作中遇到合并数据库的情况,他是通过expdp提取出五个库对象,然后impdp到新库里面.我觉得这种方法特别耗时,尤其在数据量比较大的时候.这种时候我觉得采用表 ...

  6. TP5.0:访问不同模块方法,跳转视图页面

    我们在开发项目时,都会给每个项目加上基本的后台管理页面,并命名为admin 那么,我们在添加admin后台模块后,怎么通过url访问admin后台模块文件代码呢? 1.访问admin模块默认文件的UR ...

  7. elasticsearch 概念

    elasticsearch 来源:https://baike.baidu.com/item/elasticsearch/3411206?fr=aladdin ElasticSearch是一个基于Luc ...

  8. POJ-3273 Monthly Expense---最小化最大值

    题目链接: https://cn.vjudge.net/problem/POJ-3273 题目大意: 给N个数,划分为M个块(不得打乱数顺序).找到一个最好的划分方式,使得块的和的最大值 最小 解题思 ...

  9. (第五场)J plan 【贪心】

    题目链接:https://www.nowcoder.com/acm/contest/143/J 题目描述 There are n students going to travel. And hotel ...

  10. 【luogu P3372 线段树1】 模板

    线段树的模板题 题目链接:https://www.luogu.org/problemnew/show/P3372 update区间修改,query区间求和 #include <iostream& ...