干货教程:python采集小说网站完整教程(附完整代码)

优采云 发布时间: 2022-11-01 22:21

  干货教程:python采集小说网站完整教程(附完整代码)

  Python 采集网站数据,本教程使用刮擦蜘蛛

  1. 安装刮擦框架

  命令行执行:

   pip install scrapy

  如果安装的抓取依赖包与你最初安装的其他python包冲突,建议使用Virtualenv进行安装

  它

  安装完成后,只需找到一个文件夹即可创建爬虫

  scrapy startproject 你的蜘蛛名称

  文件夹目录

  爬虫

  规则写在爬虫目录中

  items.py – 需要爬网的数据

  pipelines.py - 执行数据保存

  设置 – 配置

  middlewares.py – 下载器

  以下是采集新颖网站的源代码

  在 items.py 中定义采集数据

  第一

  # author 小白

import scrapy

class BookspiderItem(scrapy.Item):

# define the fields for your item here like:

i = scrapy.Field()

book_name = scrapy.Field()

book_img = scrapy.Field()

book_author = scrapy.Field()

book_last_chapter = scrapy.Field()

book_last_time = scrapy.Field()

book_list_name = scrapy.Field()

book_content = scrapy.Field()

pass

  编写采集规则

  # author 小白

<p>

import scrapy

from ..items import BookspiderItem

class Book(scrapy.Spider):

name = "BookSpider"

start_urls = [

&#39;http://www.xbiquge.la/xiaoshuodaquan/&#39;

]

def parse(self, response):

bookAllList = response.css(&#39;.novellist:first-child>ul>li&#39;)

for all in bookAllList:

booklist = all.css(&#39;a::attr(href)&#39;).extract_first()

yield scrapy.Request(booklist,callback=self.list)

def list(self,response):

book_name = response.css(&#39;#info>h1::text&#39;).extract_first()

book_img = response.css(&#39;#fmimg>img::attr(src)&#39;).extract_first()

book_author = response.css(&#39;#info p:nth-child(2)::text&#39;).extract_first()

book_last_chapter = response.css(&#39;#info p:last-child::text&#39;).extract_first()

book_last_time = response.css(&#39;#info p:nth-last-child(2)::text&#39;).extract_first()

bookInfo = {

&#39;book_name&#39;:book_name,

&#39;book_img&#39;:book_img,

&#39;book_author&#39;:book_author,

&#39;book_last_chapter&#39;:book_last_chapter,

&#39;book_last_time&#39;:book_last_time

}

list = response.css(&#39;#list>dl>dd>a::attr(href)&#39;).extract()

i = 0

for var in list:

i += 1

bookInfo[&#39;i&#39;] = i # 获取抓取时的顺序,保存数据时按顺序保存

yield scrapy.Request(&#39;http://www.xbiquge.la&#39;+var,meta=bookInfo,callback=self.info)

def info(self,response):

self.log(response.meta[&#39;book_name&#39;])

content = response.css(&#39;#content::text&#39;).extract()

item = BookspiderItem()

item[&#39;i&#39;] = response.meta[&#39;i&#39;]

  

item[&#39;book_name&#39;] = response.meta[&#39;book_name&#39;]

item[&#39;book_img&#39;] = response.meta[&#39;book_img&#39;]

item[&#39;book_author&#39;] = response.meta[&#39;book_author&#39;]

item[&#39;book_last_chapter&#39;] = response.meta[&#39;book_last_chapter&#39;]

item[&#39;book_last_time&#39;] = response.meta[&#39;book_last_time&#39;]

item[&#39;book_list_name&#39;] = response.css(&#39;.bookname h1::text&#39;).extract_first()

item[&#39;book_content&#39;] = &#39;&#39;.join(content)

yield item

</p>

  保存数据

  import os

class BookspiderPipeline(object):

def process_item(self, item, spider):

curPath = &#39;E:/小说/&#39;

tempPath = str(item[&#39;book_name&#39;])

targetPath = curPath + tempPath

if not os.path.exists(targetPath):

os.makedirs(targetPath)

book_list_name = str(str(item[&#39;i&#39;])+item[&#39;book_list_name&#39;])

filename_path = targetPath+&#39;/&#39;+book_list_name+&#39;.txt&#39;

print(&#39;------------&#39;)

print(filename_path)

with open(filename_path,&#39;a&#39;,encoding=&#39;utf-8&#39;) as f:

f.write(item[&#39;book_content&#39;])

return item

  执行

  scrapy crawl BookSpider

  完成新节目的采集

  这里推荐

  scrapy shell 爬取的网页url

  然后 response.css(&#39;&#39;) 测试规则是否正确

  这里我还是要推荐我自己的Python开发学习小组:810735403,这个小组正在学习Python开发,如果你正在学习Python,欢迎加入,大家都是一个软件开发方,不时分享干货(只和Python软件开发相关),包括我自己编了2020年最新的Python高级信息和高级开发教程, 欢迎来到高级,并希望深入了解 Python 合作伙伴!

  内容分享:2522期:影视解说文案*敏*感*词*:自动采集 一键伪原创 打造爆款文案(工具+解说稿

  免费下载或者VIP会员资源可以直接商业化吗?

  本站所有资源版权归原作者所有。此处提供的资源仅供参考和学习使用,请勿直接用于商业用途。如因商业用途发生版权纠纷,一切责任由用户承担。更多信息请参考VIP介绍。

  提示下载完成但无法解压或打开?

  

  最常见的情况是下载不完整:可以将下载的压缩包与网盘容量进行对比。如果小于网盘指示的容量,就是这个原因。这是浏览器下载bug,建议使用百度网盘软件或迅雷下载。如果排除了这种情况,可以在对应资源底部留言,或者联系我们。

  在资产介绍文章 中找不到示例图片?

  对于会员制、全站源代码、程序插件、网站模板、网页模板等各类素材,文章中用于介绍的图片通常不收录在相应的下载中材料包。这些相关的商业图片需要单独购买,本站不负责(也没有办法)找到来源。某些字体文件也是如此,但某些资产在资产包中会有字体下载链接列表。

  付款后无法显示下载地址或无法查看内容?

  

  如果您已经支付成功但网站没有弹出成功提示,请联系站长提供支付信息供您处理

  购买此资源后可以退款吗?

  源材料是一种虚拟商品,可复制和传播。一经批准,将不接受任何形式的退款或换货请求。购买前请确认您需要的资源

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线