通過官方嘖嘖,我決定嘗試建立自己的蜘蛛在同一個項目之後。我在蜘蛛目錄中創建了parker_spider.py。其中包含:
start_urls = [
"myurl"
]
class Parker_Spider(scrapy.Spider):
name = "parker"
def start_requests(self):
for i in range(self.max_id):
yield Request('myurl', method="post", headers= headers, body=payload, callback=self.parse_method)
def parse_method(self, response):
j = json.loads(response.body_as_unicode())
print(j['d'][0])
我可以看到正確的輸出打印出來的蜘蛛運行,所以我知道它的工作。現在我想將輸出存儲爲JSON。當我運行時:
$ scrapy crawl parker -o items.json
............
2016-05-31 16:53:55 [scrapy] INFO: Closing spider (finished)
2016-05-31 16:53:55 [scrapy] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 16112,
'downloader/request_count': 26,
'downloader/request_method_count/POST': 26,
'downloader/response_bytes': 12484,
'downloader/response_count': 26,
'downloader/response_status_count/200': 26,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2016, 5, 31, 20, 53, 55, 192000),
'log_count/DEBUG': 27,
'log_count/INFO': 7,
'response_received_count': 26,
'scheduler/dequeued': 26,
'scheduler/dequeued/memory': 26,
'scheduler/enqueued': 26,
'scheduler/enqueued/memory': 26,
'start_time': datetime.datetime(2016, 5, 31, 20, 53, 54, 31000)}
2016-05-31 16:53:55 [scrapy] INFO: Spider closed (finished)
items.json是在項目目錄中創建的,但它是空的。我究竟做錯了什麼?
編輯:改變蜘蛛的代碼如下:
def parse_method(self, response):
j = json.loads(response.body_as_unicode())
ParkerItem.account=j['d'][0]
print(j['d'][0])
return ParkerItem.account
items.py:
class ParkerItem(scrapy.Item):
account = scrapy.Field()
現在,當我運行它,我得到:
ERROR: Spider must return Request, BaseItem, dict or None, got 'unicode' in <POST myurl
現在怎麼辦?
謝謝!一個問題:爲什麼在parse_method函數中可用的項目?我不會導入它。 – user61629