笔趣阁是很好爬的网站了,这里简单爬取了全部小说链接和每本的全部章节链接,还想爬取章节内容在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. Android学习——ViewPager的使用(三)

    这一节来介绍一下在ViewPager中常用到的一个控件,标题栏. 标题栏分为PagerTabStrip和PagerTitleStrip两种,用法类似,这里介绍第一种. 具体做法 在layout文件中的 ...

  2. java面试题之----HashMap常见面试题总结

    “你用过HashMap吗?” “什么是HashMap?你为什么用到它?” 几乎每个人都会回答“是的”,然后回答HashMap的一些特性,譬如HashMap可以接受null键值和值,而Hashtable ...

  3. 一个sql server 实施工程师的反思

    自14年开始从事数据库实施,至今(2018-02-16)晃眼间已经四个年头过去了,工作上的能力要求多数能自己解决,可这不应该成为我学习路上的终点.加之总觉得自己对sql 的理解有些浮于表面,所以借着春 ...

  4. May 17th 2017 Week 20th Wednesday

    Men are nearly always willing to believe what they wish. 人总爱想入非非,把愿望变成现实. It is just the humancondit ...

  5. March 15 2017 Week 11 Wednesday

    The starting point of all achievements is desire. 成功的第一步是渴望. Only you desire for somethings, you can ...

  6. MySQL学习(二)数据类型

    截取书中内容留作学习.... 1.整数类型 2.浮点数与定点数类型 3.日期时间类型 向数据库中插入当前系统时间:CURRENT_TIME或者NOW() 4.文本字符串类型 MySQL枚举类型:cre ...

  7. SSD 从形式到实质之改变

    SSD 从形式到实质之改变  作者:廖恒          SSD的物理尺寸之混战正在进行其中. 数据中心的硬件架构师由于要规划下一代server的机械设计.还要制定JBOD的设计规范,想必面临不少困 ...

  8. POJ 1579 Function Run Fun 【记忆化搜索入门】

    题目传送门:http://poj.org/problem?id=1579 Function Run Fun Time Limit: 1000MS   Memory Limit: 10000K Tota ...

  9. GroundPlaneEstimator.cpp解读

    GroundPlaneEstimator域下的compute函数,就相当于整个cpp的主函数,也体现了整个调用过程,先执行compute_v_disparity_data,再compute_v_dis ...

  10. 【洛谷P3853】 [TJOI2007]路标设置

    路标设置 题目链接 此题和跳石头很相似,都是二分答案,模拟判断是否可行 #include<iostream> #include<cstdio> using namespace ...