sách gpt4 ai đã đi

python - 垃圾。开始爬行后如何更改蜘蛛设置?

In lại Tác giả: Space Dog 更新时间:2023-10-29 21:55:37 31 4
mua khóa gpt4 Nike

我无法在解析方法中更改蜘蛛设置。但这绝对是一种方式。

Ví dụ:

class SomeSpider(BaseSpider): name = 'mySpider' allowed_domains = ['example.com'] start_urls = ['http://example.com'] settings.overrides['ITEM_PIPELINES'] = ['myproject.pipelines.FirstPipeline'] print settings['ITEM_PIPELINES'][0] #printed 'myproject.pipelines.FirstPipeline' def parse(self, response): #...some code settings.overrides['ITEM_PIPELINES'] = ['myproject.pipelines.SecondPipeline'] print settings['ITEM_PIPELINES'][0] # printed 'myproject.pipelines.SecondPipeline' item = Myitem() item['mame'] = 'Name for SecondPipeline'  

但是!项目将由 FirstPipeline 处理。新的 ITEM_PIPELINES 参数不起作用。开始抓取后如何更改设置?提前致谢!

1 Câu trả lời

如果您希望不同的蜘蛛具有不同的管道,您可以为蜘蛛设置管道列表属性,该属性定义该蜘蛛的管道。比在管道中检查是否存在:

class MyPipeline(object):

def process_item(self, item, spider):
if self.__class__.__name__ not in getattr(spider, 'pipelines',[]):
return item
...
return item

class MySpider(CrawlSpider):
pipelines = set([
'MyPipeline',
'MyPipeline3',
])

如果您希望不同的项目由不同的管道处理,您可以这样做:

    class MyPipeline2(object):
def process_item(self, item, spider):
if isinstance(item, MyItem):
...
return item
return item

关于python - 垃圾。开始爬行后如何更改蜘蛛设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10543997/

31 4 0
Bài viết được đề xuất: python - random.sample 的时间复杂度
Bài viết được đề xuất: c# - 尝试访问 WCF 服务时出现最新的 MonoTouch 4.0 问题
Bài viết được đề xuất: c# - 获取所有线程的堆栈跟踪
Bài viết được đề xuất: python - 在 Ubuntu 上使用 Access 数据库 (.mdb) 和 Python
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com