1.爬取目标网站:http://www.zhaoxiaoshuo.com/all.php?c=0&o=0&s=0&f=2&l=0&page=1

2.爬取目标网站信息:小说类型  小说书名  小说作者  小说字数  小说投票数  小说搜藏数  小说状态

3.scrapy框架结构:

zhaoxiaoshuo
zhaoxiaoshuo
spiders
__init__.py
zhaoxiaoshuo.py
items.py
middlewares.py
pipelines.py
settings.py
__init__.py
scrapy.cfg

(1)items.py

import scrapy

class ZhaoxiaoshuoItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
# 小说类别
book_category = scrapy.Field()
# 小说书名
book_name = scrapy.Field()
# 小说作者
book_author = scrapy.Field()
# 小说字数
book_words = scrapy.Field()
# 小说投票数
book_vote = scrapy.Field()
# 小说收藏数
book_collection = scrapy.Field()
# 小说状态
book_status = scrapy.Field()

(2)spiders/zhaoxiaoshuo.py

import scrapy
from scrapy.http import Request
from bs4 import BeautifulSoup
import re
from zhaoxiaoshuo.items import ZhaoxiaoshuoItem class ZhaoXiaoShuo(scrapy.Spider):
name = "zhaoxiaoshuo"
allowed_domains = ['zhaoxiaoshuo.com']
first_url = 'http://www.zhaoxiaoshuo.com'
base_url = 'http://www.zhaoxiaoshuo.com/all.php?c={}&o=0&s=0&f=2&l=0&page=1' def start_requests(self):
for i in range(2, 22):
url = self.base_url.format(str(i))
yield Request(url, self.get_max_page, meta={
'url': url
})
yield Request(self.base_url.format(str(0)), self.get_max_page, meta={
'url': self.base_url.format(str(0))
}) def get_max_page(self, response):
soup = BeautifulSoup(response.text, "lxml")
max_page = int(re.search("\d+", soup.select(".pages a")[4].text).group())
url = response.meta['url']
for page in range(1, max_page + 1):
url = url.replace("page=1", "page={}".format(str(page)))
yield Request(url, self.parse) def parse(self, response):
soup = BeautifulSoup(response.text, "lxml")
ul = soup.select(".clearfix")[2]
lis = ul.select("li")
for li in lis:
# category = li.select(".width369")[0].text.strip()
name = li.select(".green")[0].text.strip()
status = li.select(".red")[0].text.strip()
author = li.select(".width111")[0].text.strip()
url = self.first_url + li.select(".green")[0]['href']
yield Request(url, self.get_information, meta={
# 'category': category,
'name': name,
'status': status,
'author': author
}) def get_information(self, response):
item = ZhaoxiaoshuoItem()
soup = BeautifulSoup(response.text, "lxml")
item['book_category'] = soup.select(".crumbswrap a")[1].text.strip()
item['book_name'] = response.meta['name']
item['book_author'] = response.meta['author']
item['book_words'] = soup.select(".r420 p span")[1].text.strip()
item['book_vote'] = soup.select(".r420 p span")[2].text.strip()
item['book_collection'] = soup.select(".r420 p span")[2].text.strip()
item['book_status'] = response.meta['status']
return item

(3)pipelines.py

因为并没有选择存储,所以没有编辑

(4)其它(默认处理)

scrapy框架爬取小说信息的更多相关文章

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

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

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

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

  3. scrapy框架爬取笔趣阁

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

  4. 爬虫入门(四)——Scrapy框架入门:使用Scrapy框架爬取全书网小说数据

    为了入门scrapy框架,昨天写了一个爬取静态小说网站的小程序 下面我们尝试爬取全书网中网游动漫类小说的书籍信息. 一.准备阶段 明确一下爬虫页面分析的思路: 对于书籍列表页:我们需要知道打开单本书籍 ...

  5. 使用scrapy框架爬取全书网书籍信息。

    爬取的内容:书籍名称,作者名称,书籍简介,全书网5041页,写入mysql数据库和.txt文件 1,创建scrapy项目 scrapy startproject numberone 2,创建爬虫主程序 ...

  6. python利用scrapy框架爬取起点

    先上自己做完之后回顾细节和思路的东西,之后代码一起上. 1.Mongodb 建立一个叫QiDian的库,然后建立了一个叫Novelclass(小说类别表)Novelclass(可以把一级类别二级类别都 ...

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

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

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

    scrapy框架是个比较简单易用基于python的爬虫框架,http://scrapy-chs.readthedocs.org/zh_CN/latest/ 这个是不错的中文文档 几个比较重要的部分: ...

  9. 基于python的scrapy框架爬取豆瓣电影及其可视化

    1.Scrapy框架介绍 主要介绍,spiders,engine,scheduler,downloader,Item pipeline scrapy常见命令如下: 对应在scrapy文件中有,自己增加 ...

随机推荐

  1. ORA-06512 问题解决

    SQL> select * from dba_role_privs where grantee='SUK';     GRANTEE GRANTED_ROLE ADMIN_OPTION DEFA ...

  2. 非定制UIImagePickerController的使用

    非定制UIImagePickerController的使用 效果: 源码: // // ViewController.m // ImagePic // // Created by XianMingYo ...

  3. Apache源码安装--httpd-2.2.34

    一.下载源码包 二.将源码包移动/usr/src/目录 三.解压源码包,并进入目录:tar -xzvf httpd-2.2.34.tar.gz,cd httpd-2.2.34 四.安装依赖包:yum ...

  4. 'Table is Marked as crashed and should be repaired Error'.Mysql表损坏解决方案

    问题表现:由于服务器崩溃导致表损坏无法打开或者能打开但是无法写入数据(提示主键重复但实际没有该主键且该主键值在最大值范围内). 本文提供两种检查修复方式:mysqlcheck 和 myisamchk ...

  5. ZT pthread_cleanup_push()/pthread_cleanup_pop()的详解

    pthread_cleanup_push()/pthread_cleanup_pop()的详解 分类: Linux 2010-09-28 16:02 1271人阅读 评论(1) 收藏 举报 async ...

  6. 1051. [HAOI2006]受欢迎的牛【强连通分量】

    Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也 ...

  7. P1439 【模板】最长公共子序列

    题目描述 给出1-n的两个排列P1和P2,求它们的最长公共子序列. 输入输出格式 输入格式: 第一行是一个数n, 接下来两行,每行为n个数,为自然数1-n的一个排列. 输出格式: 一个数,即最长公共子 ...

  8. shell基础--变量的数值计算

    变量的数值计算 1.$((表达式)) (1).实验1 [root@~_~ day4]# cat test.sh #!/bin/bash a=6 b=2 echo "a-b=$(($a-$b) ...

  9. playbook-拓展

    一.Handles 1. Handlers 在发生改变时执行的操作 2. handlers也是一些task的列表,通过名字来引用,它们和一般的task并没有什么区别. 3. handlers是由not ...

  10. 【转】Android 内核初识(6)SystemServer进程

    简介 SystemServer的进程名实际上叫做“system_server”,通常简称为SS. 系统中的服务驻留在其中,常见的比如WindowManagerServer(Wms).ActivityM ...