Python scrapy爬取起点中文网小说榜单

作者:超哥-- 时间:2022-11-06 14:34:11 

一、项目需求

爬取排行榜小说的作者,书名,分类以及完结或连载

二、项目分析

目标url:“https://www.qidian.com/rank/hotsales?style=1&page=1”

Python scrapy爬取起点中文网小说榜单

通过控制台搜索发现相应信息均存在于html静态网页中,所以此次爬虫难度较低。

Python scrapy爬取起点中文网小说榜单

通过控制台观察发现,需要的内容都在一个个li列表中,每一个列表代表一本书的内容。

Python scrapy爬取起点中文网小说榜单

在li中找到所需的内容

Python scrapy爬取起点中文网小说榜单

找到第两页的url
“https://www.qidian.com/rank/hotsales?style=1&page=1”
“https://www.qidian.com/rank/hotsales?style=1&page=2”
对比找到页数变化
开始编写scrapy程序。

三、程序编写

创建项目太简单,不说了

1.编写item(数据存储)


import scrapy

class QidianHotItem(scrapy.Item):
   name = scrapy.Field() #名称
   author = scrapy.Field() #作者
   type = scrapy.Field() #类型
   form= scrapy.Field() #是否完载

2.编写spider(数据抓取(核心代码))


#coding:utf-8

from scrapy import Request
from scrapy.spiders import Spider
from ..items import QidianHotItem
#导入下需要的库

class HotSalesSpider(Spider):#设置spider的类
   name = "hot" #爬虫的名称
   qidian_header={"user-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"} #设置header
   current_page = 1 #爬虫起始页
   def start_requests(self): #重写第一次请求
       url="https://www.qidian.com/rank/hotsales?style=1&page=1"
       yield Request(url,headers=self.qidian_header,callback=self.hot_parse)
#Request发起链接请求
#url:目标url
#header:设置头部(模拟浏览器)
#callback:设置页面抓起方式(空默认为parse)
   def hot_parse(self, response):#数据解析
       #xpath定位
       list_selector=response.xpath("//div[@class='book-mid-info']")
       #获取所有小说
       for one_selector in list_selector:
           #获取小说信息
           name=one_selector.xpath("h4/a/text()").extract()[0]
           #获取作者
           author=one_selector.xpath("p[1]/a[1]/text()").extract()[0]
           #获取类型
           type=one_selector.xpath("p[1]/a[2]/text()").extract()[0]
           # 获取形式
           form=one_selector.xpath("p[1]/span/text()").extract()[0]

item = QidianHotItem()
           #生产存储器,进行信息存储
           item['name'] = name
           item['author'] = author
           item['type'] = type
           item['form'] = form

yield item #送出信息

# 获取下一页URL,并生成一个request请求
           self.current_page += 1
           if self.current_page <= 10:#爬取前10页
               next_url = "https://www.qidian.com/rank/hotsales?style=1&page="+str(self.current_page)
               yield Request(url=next_url,headers=self.qidian_header,callback=self.hot_parse)

def css_parse(self,response):
       #css定位
       list_selector = response.css("[class='book-mid-info']")
       for one_selector in list_selector:
           # 获取小说信息
           name = one_selector.css("h4>a::text").extract()[0]
           # 获取作者
           author = one_selector.css(".author a::text").extract()[0]
           # 获取类型
           type = one_selector.css(".author a::text").extract()[1]
           # 获取形式
           form = one_selector.css(".author span::text").extract()[0]
           # 定义字典

item=QidianHotItem()
           item['name']=name
           item['author'] = author
           item['type'] = type
           item['form'] = form
           yield  item

3.start.py(代替命令行)

在爬虫项目文件夹下创建start.py。

Python scrapy爬取起点中文网小说榜单


from scrapy import cmdline
#导入cmd命令窗口
cmdline.execute("scrapy crawl hot -o hot.csv" .split())
#运行爬虫并生产csv文件

出现类似的过程代表爬取成功。

Python scrapy爬取起点中文网小说榜单

hot.csv

Python scrapy爬取起点中文网小说榜单

来源:https://blog.csdn.net/weixin_50835854/article/details/117783644

标签:Python,爬取,小说
0
投稿

猜你喜欢

  • golang 实用库gotable的具体使用

    2024-04-27 15:31:48
  • Python3爬虫中关于Ajax分析方法的总结

    2021-04-07 17:28:47
  • Python切割图片成九宫格的示例代码

    2023-07-10 07:00:57
  • ORACLE常见错误代码的分析与解决(三)

    2024-01-17 19:17:57
  • Django项目实战之配置文件详解

    2023-08-13 19:28:07
  • Springboot如何同时装配两个相同类型数据库

    2024-01-25 08:51:33
  • 使用python如何提取JSON数据指定内容

    2022-08-06 23:48:41
  • GoLand 2020.3 正式发布有不少新功能(支持泛型)

    2024-04-25 15:27:40
  • 使用pkg打包Node.js应用的方法步骤

    2024-05-13 09:58:30
  • YOLOv5车牌识别实战教程(二)理论基础

    2021-11-08 09:16:06
  • 关于python 的legend图例,参数使用说明

    2022-07-21 10:10:13
  • SQL Server 2005的cmd_shell组件的开启方法

    2024-01-19 15:18:06
  • Python参数传递实现过程及原理详解

    2021-07-11 20:16:07
  • 如何决定是否将登录内容保存到Cookie里?

    2009-12-16 18:54:00
  • Python Django切换MySQL数据库实例详解

    2024-01-21 02:02:47
  • Python基于Faker假数据构造库

    2021-06-05 15:32:19
  • Python封装zabbix-get接口的代码分享

    2021-12-05 08:57:39
  • 为Python的web框架编写MVC配置来使其运行的教程

    2022-05-30 01:54:32
  • 一篇文章弄懂Python中所有数组数据类型

    2023-01-12 18:25:05
  • Golang json 库中的RawMessage功能原理

    2024-04-30 10:07:27
  • asp之家 网络编程 m.aspxhome.com