
本文分析一个Scrapy爬虫项目中,管道(Pipeline)持久化存储文件写入失败的问题。问题表现为:创建的文件为空,无法写入数据。
代码示例(问题代码):
# spider.py
import scrapy
import sys
sys.path.append(r'd:\project_test\pydemo\demo1\xunlian\myspider\qiubai')
from ..items import qiubaiitem
class biedouspider(scrapy.Spider):
name = "biedou"
start_urls = ["https://www.biedoul.com/wenzi/"]
def parse(self, response):
dl_list = response.xpath('/html/body/div[4]/div[1]/div[1]/dl')
for dl in dl_list:
title = dl.xpath('./span/dd/a/strong/text()')[0].extract()
content = dl.xpath('./dd//text()').extract()
content = ''.join(content)
item = qiubaiitem()
item['title'] = title
item['content'] = content
yield item
break
# items.py
import scrapy
class qiubaiitem(scrapy.Item):
title = scrapy.Field()
content = scrapy.Field()
# pipelines.py
class qiubaipipeline(object):
def __init__(self):
self.fp = None
def open_spdier(self, spider): # 拼写错误!
print("开始爬虫")
self.fp = open('./biedou.txt', 'w', encoding='utf-8')
def close_spider(self, spider):
print("结束爬虫")
self.fp.close()
def process_item(self, item, spider):
title = str(item['title'])
content = str(item['content'])
self.fp.write(title + ':' + content + '\n')
return item错误信息:
<code>... typeerror: object of type qiubaiitem is not json serializable 结束爬虫 ... attributeerror: 'nonetype' object has no attribute 'close'</code>
问题分析:
错误信息提示'nonetype' object has no attribute 'close',表明self.fp为None,导致无法关闭文件。 进一步追溯原因,发现pipelines.py文件中open_spdier方法名拼写错误,应为open_spider。 由于方法名错误,Scrapy框架无法正确调用open_spider方法打开文件,导致self.fp始终为None。 TypeError错误则是因为process_item方法尝试写入qiubaiitem对象,而该对象并非直接可写入字符串类型。
解决方案:
修正pipelines.py文件中open_spdier方法的拼写错误,并改进process_item方法以正确处理Item对象:
# pipelines.py (修正后的代码)
class QiubaiPipeline(object):
def __init__(self):
self.fp = None
def open_spider(self, spider): # 修正拼写错误
print("开始爬虫")
self.fp = open('./biedou.txt', 'w', encoding='utf-8')
def close_spider(self, spider):
print("结束爬虫")
self.fp.close()
def process_item(self, item, spider):
title = item['title']
content = item['content']
self.fp.write(f"{title}:{content}\n") # 使用f-string更简洁
return item通过修正方法名和改进process_item方法,确保文件被正确打开和写入数据,从而解决文件写入失败的问题。 建议使用f-string格式化字符串,代码更简洁易读。 此外,为了更好的错误处理,建议添加try...except块来处理潜在的IO错误。
以上就是Scrapy爬虫管道持久化存储文件无法写入的原因是什么?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号