books

 TOM 给出了他认为DBA应该阅读的Document 的

1。Concept Guild -- 基本概念是基础,基本概念不理解,其它的根本不可能理解好
2。New Feutrue Guild -- 新功能很重要
3。Backup & Recovery Concept --  对于DBA来说,保证数据安全是最重要的
4。Recovery Manager Reference -- RMAN是对ORACLE进行备份恢复的最佳选择
5。Backup & Recovery Concept -- 再次强调,DB速度可以慢点,偶尔Down 机也没办法,丢数据才是最

大的罪过……
6。Administrator Guild  -- 对DB进行管理,当然DBA也是最重要的职能
7。Performance Tuning Guild and Reference -- 性能调整也必不可少

 

concepts,administrator guide,performance turning guide

10g completely reference

10g plsql programming

think in java

 

Pro Oracle Database 10g RAC on Linux: Installation, Administration, and Performance:

http://www.ebooksboard.com/pro-oracle-database-10g-rac-on-linux-installation-administration-and-performance/

<think>我们分析用户的问题:在Scrapy管道和设置代码中,books文件夹未显示。用户提供了两个Pipeline类和一个设置项。 用户的问题可能是:在运行Scrapy爬虫后,期望将图片下载到`./books/`文件夹,但该文件夹没有生成或没有文件。 观察用户提供的代码: 1. `DdangPipeline`:负责将item写入到'book.json'文件。在open_spider时打开文件,process_item中写入item字符串,close_spider时关闭文件。 2. `DdangdownloadPipeline`:负责下载图片。在process_item中,从item中获取'src'和'name',拼接URL和文件名,然后使用urllib.request.urlretrieve下载图片到'./books/'目录下。 3. 在settings.py中,设置了两个管道,且`DdangdownloadPipeline`的优先级为301(在`DdangPipeline`之后执行)。 可能的问题原因: 1. 文件夹不存在:代码中直接使用`filename='./books/'+item.get('name')+'.jpg'`,如果`./books/`目录不存在,则下载会失败,但不会抛出异常(urllib.request.urlretrieve在目标目录不存在时会抛出FileNotFoundError)。 2. item中缺少必要的字段:在`DdangdownloadPipeline`中使用了`item.get('src')`和`item.get('name')`,如果这两个字段在item中不存在,那么获取到的就是None,拼接出的URL和文件名就会有问题。 3. 管道执行顺序:由于两个管道都返回了item,所以顺序没有问题,`DdangdownloadPipeline`会在`DdangPipeline`之后处理item。 4. 异常处理:如果下载过程中出现异常(如网络问题、URL不完整等),可能导致下载失败。 解决方案: 1. 确保`books`目录存在:在下载之前,先检查目录是否存在,如果不存在则创建。可以使用os.makedirs来创建目录。 2. 检查item中是否包含'src'和'name'字段:可以在下载之前添加判断,如果缺少字段则跳过或记录错误。 3. 添加异常处理:在下载时使用try-except来捕获异常,避免因为一个下载失败导致整个管道出错。 4. 日志记录:在关键步骤添加日志,便于调试。 修改`DdangdownloadPipeline`的代码,添加目录创建和异常处理: 首先,在管道中导入必要的模块: import os import urllib.request import logging 然后,修改`DdangdownloadPipeline`: 方法一:在`open_spider`方法中创建目录(这样只需要创建一次): def open_spider(self, spider): # 检查books目录是否存在,不存在则创建 if not os.path.exists('./books'): os.makedirs('./books') 方法二:在`process_item`中每次检查(效率稍低,但更安全): def process_item(self, item, spider): # 确保目录存在 if not os.path.exists('./books'): os.makedirs('./books') ... 后续下载代码 另外,建议在下载时检查字段是否存在: if not item.get('src') or not item.get('name'): logging.error("Item missing 'src' or 'name' field") return item 完整的修改后的`DdangdownloadPipeline`: ```python import os import urllib.request import logging class DdangdownloadPipeline: def open_spider(self, spider): # 创建books目录(如果不存在) if not os.path.exists('./books'): os.makedirs('./books') def process_item(self, item, spider): # 检查item中是否包含必要的字段 if 'src' not in item or 'name' not in item: logging.warning("Item missing 'src' or 'name' field, skipping download.") return item url = 'http:' + item['src'] # 文件名中可能包含特殊字符或路径分隔符,需要清理(这里简单处理,实际可能需要更严格的清洗) # 注意:文件名不能包含Windows/Linux的非法字符,这里用替换路径分隔符为例 filename = './books/' + item['name'].replace('/', '_') + '.jpg' try: urllib.request.urlretrieve(url, filename) logging.info(f"Downloaded {url} to {filename}") except Exception as e: logging.error(f"Download failed for {url}: {str(e)}") return item ``` 注意:在Windows系统中,文件名不能包含\/:*?"<>|等字符,所以最好对文件名进行清理。这里简单地将'/'替换为下划线,实际可能需要更全面的清理。 另外,在settings.py中确保启用了该管道,并且优先级设置正确(已经设置)。 如果问题仍然存在,可以检查: - 爬虫中是否确实产生了'src'和'name'字段。 - 图片URL是否完整(加上'http:'前缀后是否有效)。 - 是否有网络问题或网站反爬。 根据引用[3]和[4]中提到,有时候管道中的open_spider和close_spider方法可能不会被调用,但这里我们使用的是Scrapy内置的管道机制,只要在settings中正确配置了管道,并且爬虫正常启动和关闭,这些方法应该会被调用。不过,为了确保,我们可以使用Scrapy的信号机制(如引用[4]所示)来连接spider_opened和spider_closed事件,但这里我们只需要在管道中重写open_spider方法,并且用于创建目录,所以即使没有调用,我们在process_item中也会检查目录。 因此,我们选择在open_spider中创建目录,同时在process_item开始时也检查一次目录(双保险)。或者,如果担心open_spider没有被调用,可以只在process_item中检查目录。 这里我们选择在open_spider中创建,因为管道一旦启用,open_spider在爬虫启动时会被调用(除非爬虫启动过程被中断)。同时,在process_item中检查目录是否存在,如果不存在则创建,这样更健壮。 修改后的代码(在open_spider和process_item中都检查)可能重复,所以我们可以只在open_spider中创建,而在process_item中不检查。但是,如果多个爬虫共用同一个管道,且某些爬虫没有调用open_spider(虽然不太可能),那么目录可能不存在。所以,为了健壮性,我们在process_item中确保目录存在。 最终代码(在process_item中确保目录存在): ```python import os import urllib.request import logging class DdangdownloadPipeline: def process_item(self, item, spider): # 确保目录存在 if not os.path.exists('./books'): os.makedirs('./books') # 检查item中是否包含必要的字段 if 'src' not in item or 'name' not in item: logging.warning("Item missing 'src' or 'name' field, skipping download.") return item url = 'http:' + item['src'] # 清理文件名中的非法字符(这里只处理路径分隔符,实际可能需要更多处理) name = item['name'] # 替换可能引起路径问题的字符 for char in ['/', '\\', ':', '*', '?', '"', '<', '>', '|']: name = name.replace(char, '_') filename = os.path.join('./books', name + '.jpg') try: urllib.request.urlretrieve(url, filename) logging.info(f"Downloaded {url} to {filename}") except Exception as e: logging.error(f"Download failed for {url}: {str(e)}") return item ``` 注意:我们添加了文件名清理,避免因为文件名中的特殊字符导致文件创建失败。 此外,在settings.py中,确保ITEM_PIPELINES设置正确,并且该管道已被启用。 如果用户按照上述修改后,应该可以解决books文件夹未显示的问题。 最后,提醒用户:如果爬虫运行后仍然没有生成文件夹或文件,请检查日志输出,看是否有警告或错误信息(如字段缺失、下载失败等)。 </think> 针对Scrapy管道中`books`文件夹未显示的问题,结合您提供的代码和引用资料,以下是解决方案和详细分析: ### 问题原因分析 1. **目录未创建**:`DdangdownloadPipeline`直接尝试写入`./books/`目录,但未检查或创建该目录(引用[5]的`__init__`方法展示了正确做法)。 2. **信号方法未注册**:`open_spider/close_spider`方法未被Scrapy自动调用(引用[3][4]提到需通过信号注册)。 3. **路径处理风险**:文件名包含特殊字符可能导致写入失败(如`item.get('name')`含`/`等符号)。 ### 修复后的完整代码 ```python # pipelines.py import os import urllib.request from scrapy import signals import logging class DdangPipeline: def open_spider(self, spider): self.fp = open('book.json', 'w', encoding='utf-8') def process_item(self, item, spider): self.fp.write(str(item)) return item def close_spider(self, spider): self.fp.close() class DdangdownloadPipeline: @classmethod def from_crawler(cls, crawler): pipeline = cls() # 注册信号确保方法被调用(关键修复) crawler.signals.connect(pipeline.spider_opened, signal=signals.spider_opened) crawler.signals.connect(pipeline.spider_closed, signal=signals.spider_closed) return pipeline def spider_opened(self, spider): # 创建books目录(关键修复) os.makedirs('./books', exist_ok=True) logging.info("创建下载目录: ./books") def spider_closed(self, spider): logging.info("爬虫关闭,释放资源") def process_item(self, item, spider): url = 'http:' + item.get('src') # 文件名安全处理(防止特殊字符) name = item.get('name').replace('/', '_').replace('\\', '_') filename = f'./books/{name}.jpg' try: urllib.request.urlretrieve(url, filename) logging.info(f"下载成功: {filename}") except Exception as e: logging.error(f"下载失败 {url}: {str(e)}") return item ``` ### settings.py 关键设置 ```python ITEM_PIPELINES = { "ddang.pipelines.DdangPipeline": 300, "ddang.pipelines.DdangdownloadPipeline": 301, } ``` ### 修复要点说明 1. **目录自动创建**: - 在`spider_opened`中使用`os.makedirs('./books', exist_ok=True)`确保目录存在 - `exist_ok=True`参数避免目录已存在时报错(引用[5]的`__init__`类似逻辑) 2. **信号注册**(引用[4]): - 通过`from_crawler`注册`spider_opened/spider_closed`信号 - 解决原代码中关闭方法不执行的问题(引用[3]) 3. **文件名安全处理**: - 使用`.replace('/', '_')`移除路径分隔符 - 防止`item.get('name')`含`/`导致创建子目录而非文件 4. **错误日志**: - 添加下载异常的详细日志记录 - 使用`logging`模块替代`print`(引用[3]) ### 验证步骤 1. 运行爬虫后检查是否生成`books`目录 2. 查看日志确认信号方法调用: ``` [scrapy.core.engine] INFO: Spider opened [root] INFO: 创建下载目录: ./books # 出现此日志说明修复成功 ``` 3. 检查`books`文件夹内是否包含下载的图片文件 > **提示**:若需资源自动释放(如数据库连接),应在`spider_closed`实现(引用[2]),本例已预留接口[^1][^2][^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值