网络编程
位置:首页>> 网络编程>> Python编程>> Python scrapy爬取起点中文网小说榜单

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

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

标签:Python,爬取,小说

一、项目需求

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

二、项目分析

目标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

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com