これは私の単純なコードであり、うまくいきません。
私はからサブクラス化していますinitspider
これは私のコードです
class MytestSpider(InitSpider):
name = 'mytest'
allowed_domains = ['example.com']
login_page = 'http://www.example.com'
start_urls = ["http://www.example.com/ist.php"]
def init_request(self):
#"""This function is called before crawling starts."""
return Request(url=self.login_page, callback=self.parse)
def parse(self, response):
item = MyItem()
item['username'] = "mytest"
return item
パイプライン
class TestPipeline(object):
def process_item(self, item, spider):
print item['username']
アイテムを印刷しようとすると、同じエラーが発生します
私が得るエラーは
File "crawler/pipelines.py", line 35, in process_item
myitem.username = item['username']
exceptions.TypeError: 'NoneType' object has no attribute '__getitem__'
私は問題がありInitSpider
ます。私のパイプラインは項目オブジェクトを取得していません
アイテム.py
class MyItem(Item):
username = Field()
設定.py
BOT_NAME = 'crawler'
SPIDER_MODULES = ['spiders']
NEWSPIDER_MODULE = 'spiders'
DOWNLOADER_MIDDLEWARES = {
'scrapy.contrib.downloadermiddleware.cookies.CookiesMiddleware': 700 # <-
}
COOKIES_ENABLED = True
COOKIES_DEBUG = True
ITEM_PIPELINES = [
'pipelines.TestPipeline',
]
IMAGES_STORE = '/var/www/htmlimages'