pyspider 使用結果

2023-02-16 16:06 更新

使用結果

從WebUI下載和查看數(shù)據(jù)很方便,但可能不適合計算機。

使用ResultDB

雖然resultdb僅用于結果預覽,但不適合大規(guī)模存儲。但是如果你想從resultdb中獲取數(shù)據(jù),有一些使用數(shù)據(jù)庫API的簡單片段可以幫助你連接和選擇數(shù)據(jù)。

```
from pyspider.database import connect_database
resultdb = connect_database("<your resutldb connection url>")
for project in resultdb.projects:
    for result in resultdb.select(project):
        assert result['taskid']
        assert result['url']
        assert result['result']
```

這result['result']是return您的腳本中的語句提交的對象。

使用ResultWorker

在產(chǎn)品環(huán)境中,您可能希望將pyspider連接到系統(tǒng)/后處理管道,而不是將其存儲到resultdb中。強烈建議覆蓋ResultWorker。

```
from pyspider.result import ResultWorker

class MyResultWorker(ResultWorker):
    def on_result(self, task, result):
        assert task['taskid']
        assert task['project']
        assert task['url']
        assert result
        # your processing code goes here
```

result是return腳本中語句提交的對象。

您可以將此腳本(例如my_result_worker.py)放在啟動pyspider的文件夾中。為result_worker子命令添加參數(shù):

```
pyspider result_worker --result-cls=my_result_worker.MyResultWorker
```

要么

```
{
  ...
  "result_worker": {
    "result_cls": "my_result_worker.MyResultWorker"
  }
  ...
}
```

如果您使用配置文件。請參閱部署

設計自己的數(shù)據(jù)庫架構

存儲在數(shù)據(jù)庫中的結果被編碼為JSON以實現(xiàn)兼容性。強烈建議您設計自己的數(shù)據(jù)庫,并覆蓋上述ResultWorker。

關于結果的提示

想要在回調中返回多個結果?

由于resultdb通過taskid(url)重復刪除結果,最新的將覆蓋以前的結果。

一種解決方法是使用send_messageAPI fake為每個結果創(chuàng)建一個taskid。

```
def detail_page(self, response):
    for li in response.doc('li').items():
        self.send_message(self.project_name, {
            ...
        }, url=response.url+"#"+li('a.product-sku').text())

def on_message(self, project, msg):
    return msg
```

另請參見:apis / self.send_message


以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號