无法访问子对象中的父实例变量

2024-04-16 06:48:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在研究scrapy框架。你知道吗

我有一些共同的属性,我想在我所有的蜘蛛,所以我做了一个蜘蛛。你知道吗

基蜘蛛

import scrapy
from src.LoggerFactory import get_logger
import ConfigParser
from redis import Redis


class BaseSpider(scrapy.Spider):
    logger = get_logger()

    def __init__(self, *args, **kwargs):
        super(scrapy.Spider, self).__init__(*args, **kwargs)
        config = ConfigParser.RawConfigParser()
        config.read('../../config.cfg')
        self.config = config
        self.redis = Redis(host=config.get('redis', 'host'), port=config.get('redis', 'port'))

    def parse(self, response):
        pass

我的EbaySpider如下

EbaySpider

import scrapy
import json
from scrapper.items import Product
from BaseSpider import BaseSpider


class EbaySpider(BaseSpider):
    name = "ebay"
    allowed_domains = ["ebay.com"]

    def __init__(self, *args, **kwargs):
        super(BaseSpider, self).__init__(*args, **kwargs)
        print self.redis # Throws AttributeError: 'EbaySpider' object has no attribute 'redis'
        exit()

最奇怪的是我仍然可以访问我的EbaySpider中的scrapy.Spider属性,尽管它不是从scrapy.Spider继承的。你知道吗

另外,如果有任何卑鄙的方式来扩展蜘蛛请建议,因为我没有找到他们的文档。你知道吗


Tags: fromimportselfredisconfiggetinitargs
1条回答
网友
1楼 · 发布于 2024-04-16 06:48:48

你的super用法是错误的。你知道吗

a typical superclass call looks like this:

class C(B):
    def method(self, arg):
        super(C, self).method(arg)

As per official 2.7 docs

你的长相在哪里

class C(B):
    def method(self, arg):
        super(B, self).method(arg)
              ^

相关问题 更多 >