笔趣阁是很好爬的网站了,这里简单爬取了全部小说链接和每本的全部章节链接,还想爬取章节内容在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. windows server 2008 64位MySQL5.6免安装版本配置说明

    1 通过官网下载MySQL5.6版本压缩包,mysql-5.6.36-winx64.zip: 2 在D盘创建目录,比如D:\MySQL,将mysql-5.6.36-winx64.zip解压缩到该目录下 ...

  2. c++链表实现学生成绩管理系统(简易版)

    #include<iostream> using namespace std; typedef struct student{ int id;//学号 string sex; string ...

  3. python、数据分析师、算法工程师的学习计划

    1.前言 最近(2018.4.1)在百忙之中开通了博客,希望能够把自己所学所想沉淀下来,这篇是我开始系统学习python,成为数据分析师和算法工程师之路的计划,望有志于为同样目标奋斗的数据猿一起交流和 ...

  4. Computer Hardware

    Computer Hardware Para 1 Computer hardware can be divides into four categories: input hardware, stor ...

  5. softmax实现cifar10分类

    将cifar10改成单一通道后,套用前面的softmax分类,分类率40%左右,想哭... .caret, .dropup > .btn > .caret { border-top-col ...

  6. POJ 1984 Navigation Nightmare 【经典带权并查集】

    任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K To ...

  7. sublime3插件安装方法

    sublime3插件安装方法:http://blog.csdn.net/u011627980/article/details/52171886

  8. 分享一个关于pthread线程栈在mm_struct里面的分布问题

    大家好,本人被下面这个问题困扰了一段时间,最近似乎找到了答案. 这里和大家分享一下,可能对有相同困惑的同学有点帮助,同时也请各位帮忙看看错漏的地方. 1================问题: 在使用p ...

  9. linux dentry cache 转自:http://blog.csdn.net/denzilxu/article/details/9188003

    Linux dentry cache学习 每个dentry对象都属于下列几种状态之一: (1)未使用(unused)状态:该dentry对象的引用计数d_count的值为0,但其d_inode指针仍然 ...

  10. 金s办公软件web前端笔试题

    1. var arr = []; arr['a'] = 1; console.log(arr.length); // A arr['4'] = 2; console.log(arr.length); ...