笔趣阁是很好爬的网站了,这里简单爬取了全部小说链接和每本的全部章节链接,还想爬取章节内容在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. solidity语言9

    输入参数 pragma solidity ^0.4.16; contract Simple { function taker(uint _a, uint _b) public pure { // do ...

  2. sqlserver broker远端端点证书认证

    1:采用windows验证的方法: CREATE ENDPOINT InstInitiatorEndpoint STATE = STARTED AS TCP ( LISTENER_PORT = ) F ...

  3. (四)WebDriver常用方法

    点击和输入 前面我们已经学习了定位元素, 定位只是第一步, 定位之后需要对这个元素进行操作, 或单击(按钮) 或输入(输入框) , 下面就来认识 WebDriver 中最常用的几个方法: clear( ...

  4. VMware,win7与linux centos6.4文件互传,linux下挂载windows共享文件夹,vmware tools安装方法

    本方法是以win7,VMware9.0.1 ,centos6.4为基础实验的. 对于linux的初级使用阶段,都会Windows中使用linux虚拟机VMWare或者其它的.在Windows与linu ...

  5. 2018_MCM_ICM_C

  6. Ajax综合应用大全(全面解析)

      AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJAX = 异步 JavaScrip ...

  7. maven学习记录四——私服 nexus

    8       私服 nexus 安装nexus 启动服务 启动失败的解决方法: 登录nexus 用户名/密码  admin/admin123 仓库类型 Virtual   虚拟仓库 Proxy  代 ...

  8. js正则判断日期

    //****************************************************************************// Function ID : Commo ...

  9. bootstrapPaginator设置同步会翻2页的小坑

    因为需要用到post的返回值在做后面的决定.不想嵌套在回调函数中.网上找了一下.设置为同步 $.ajaxSetup( { async: false }); 结果bootstrap的翻页组件就出了bug ...

  10. 对象的比较与排序(三):实现IComparable<T>和IComparer<T>泛型接口

    来源:http://www.cnblogs.com/eagle1986/archive/2011/12/06/2278531.html 1:比较和排序的概念 比较:两个实体类之间按>,=,< ...