32406

AttributeError: 'Spider' object has no attribute 'table'

Question:

I'm working with scrapy. I have a spider that starts with:

class For_Spider(Spider): name = "for" # table = 'hello' # creating dummy attribute. will be overwritten def start_requests(self): self.table = self.dc # dc is passed in

I have the following pipeline :

class DynamicSQLlitePipeline(object): @classmethod def from_crawler(cls, crawler): # Here, you get whatever value was passed through the "table" parameter table = getattr(crawler.spider, "table") return cls(table) def __init__(self,table): try: db_path = "sqlite:///"+settings.SETTINGS_PATH+"\\data.db" db = dataset.connect(db_path) table_name = table[0:3] # FIRST 3 LETTERS self.my_table = db[table_name]

When I start the spider with:

scrapy crawl for -a dc=input_string -a records=1

I get:

AttributeError: 'For_Spider' object has no attribute 'table'

If I uncomment 'table' , the program will start. I'm confused about why 'table' works but self.table does not. Can someone explain this?

Answer1:

table will work because it is a class attribute of For_Spider and self.table is just inside the function scope. self indicates the instance itself, so in that case inside the function you don't need to use it (unless you define it in __init__).<br /> If you'll try defining self.table outside the function scope you'll get an error.

Also, try using __dict__ on both classes to see their attributes and functions<br /> With table commented:<br />

<blockquote>

{'<strong>doc</strong>': None, 'start_requests': , 'name': 'for', '<strong>module</strong>': 'builtins'})

</blockquote>

As you can see, no table attribute

With table not commented:

<blockquote>

{'<strong>doc</strong>': None, 'start_requests': , 'table': 'hello', 'name': 'for', '<strong>module</strong>': 'builtins'})

</blockquote>

I hope that was clear :>

Recommend