scrapy框架爬取小说信息

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)其它(默认处理)

原文地址:https://www.cnblogs.com/loveprogramme/p/9419539.html

时间: 2024-08-29 21:45:24

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

03_使用scrapy框架爬取豆瓣电影TOP250

前言: 本次项目是使用scrapy框架,爬取豆瓣电影TOP250的相关信息.其中涉及到代理IP,随机UA代理,最后将得到的数据保存到mongoDB中.本次爬取的内容实则不难.主要是熟悉scrapy相关命令以及理解框架各部分的作用. 1.本次目标 爬取豆瓣电影TOP250的信息,将得到的数据保存到mongoDB中. 2.准备工作 需要安装好scrapy以及mongoDB,安装步骤这里不做赘述.(这里最好是先了解scrapy框架各个部分的基本作用和基础知识,这样方便后面的内容的理解.scrapy文档

python 使用scrapy框架爬取一个图书网站的信息

1.新建项目 scrapy start_project book_project 2.编写items类 3.编写spider类 # -*- coding: utf-8 -*- import scrapy from book_project.items import BookItem class BookInfoSpider(scrapy.Spider): name = "bookinfo"#定义爬虫的名字 allowed_domains = ["allitebooks.com

python利用scrapy框架爬取起点

先上自己做完之后回顾细节和思路的东西,之后代码一起上. 1.Mongodb 建立一个叫QiDian的库,然后建立了一个叫Novelclass(小说类别表)Novelclass(可以把一级类别二级类别都存进去:玄幻--一级类别,东方玄幻--二级类别)的表 client = pymongo.MongoClient(host="127.0.0.1") db = client.QiDian collection = db.Novelclass 2.用parse回调方法,获得一级类别.循环取出(

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

1.Scrapy框架介绍 主要介绍,spiders,engine,scheduler,downloader,Item pipeline scrapy常见命令如下: 对应在scrapy文件中有,自己增加爬虫文件,系统生成items,pipelines,setting的配置文件就这些. items写需要爬取的属性名,pipelines写一些数据流操作,写入文件,还是导入数据库中.主要爬虫文件写domain,属性名的xpath,在每页添加属性对应的信息等. movieRank = scrapy.Fie

使用scrapy框架爬取蜂鸟论坛的摄影图片并下载到本地

目标网站:http://bbs.fengniao.com/使用框架:scrapy 因为有很多模块的方法都还不是很熟悉,所有本次爬虫有很多代码都用得比较笨,希望各位读者能给处意见 首先创建好爬虫项目,并使用crawl模板创建爬虫文件 通过观察论坛的规律得出,很多贴子的页数往往大于一页,那么要将贴子里各页的图片下载到同一文件夹内,并且不能重名,就是获取到当前的页码数,已页码数+自然数的方式命令文件.发现scrapy自动爬虫会爬很多重复的页面,度娘后得出两个解决方法,第一个是用布隆过滤器,布隆过滤器相

scrapy框架爬取豆瓣读书(1)

1.scrapy框架 Scrapy,Python开发的一个快速.高层次的屏幕抓取和web抓取框架,用于抓取web站点并从页面中提取结构化的数据.Scrapy用途广泛,可以用于数据挖掘.监测和自动化测试. Scrapy吸引人的地方在于它是一个框架,任何人都可以根据需求方便的修改.它也提供了多种类型爬虫的基类,如BaseSpider.sitemap爬虫等,最新版本又提供了web2.0爬虫的支持. 主要组件: 2.快速开始 scrapy startproject douban cd到douban根目录

scrapy框架爬取糗事百科

在编写案例之前首先理解几个问题,1:什么是爬虫2:为什么说python是门友好的爬虫语言?3:选用哪种框架编写爬虫程序 一:什么是爬虫? 爬虫 webSpider 也称之为网络蜘蛛,是使用一段编写好的代码所生成的应用程序使其游弋于互联网这个庞大的体系中,帮助我们将想要的内容从目标服务器中搬到我们本地,通 过解析将所需要的数据结构化入库,为企业或个人决策提供依据.比如股票走势,某产品在淘宝上现存的经销商数量及销量等等 二:为什么说python是门友好的爬虫语言? 语言只是门工具,不同的语言侧重的领

用scrapy框架爬取映客直播用户头像

1. 创建项目 scrapy startproject yingke cd yingke 2. 创建爬虫  scrapy genspider live 3. 分析http://www.inke.cn/hotlive_list.html网页的response,找到响应里面数据的规律,并找到的位置,通过response.xpath()获取到 4. 通过在pipline里面进行数据的清洗,过滤,保存 5. 实现翻页,进行下一页的请求处理 6. 运行爬虫 scrapy crawl live 说明:这个程

爬虫 Scrapy框架 爬取图虫图片并下载

items.py,根据需求确定自己的数据要求 1 # -*- coding: utf-8 -*- 2 3 # Define here the models for your scraped items 4 # 5 # See documentation in: 6 # https://doc.scrapy.org/en/latest/topics/items.html 7 8 import scrapy 9 10 11 class TodayScrapyItem(scrapy.Item): 12