如何获取scrapyrt的POST meta数据?

ldfqzlk8  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(113)

在scrapyrt的POST文档中,我们可以像这样传递JSON请求,但是如何访问start_requests中的category和item之类的 meta数据呢?

{
     "request": {
         "meta": {
            "category": "some category",
            "item": {
                "discovery_item_id": "999"
            }
        },
        , "start_requests": true
    },
    "spider_name": "target.com_products"
}

参考:https://scrapyrt.readthedocs.io/en/latest/api.html#id1

9rygscc1

9rygscc11#

在scrapyRT中有一个未合并的PR,它增加了对在POST请求中传递额外参数的支持。
1)补丁resources.py文件位于scrapyrt文件夹。在我的例子中是/usr/local/lib/python3.5/dist-packages/scrapyrt/resources.py
替换为以下代码:https://github.com/gdelfresno/scrapyrt/commit/ee3be051ea647358a6bb297632d1ea277a6c02f8
2)现在,spider可以使用self.param1访问新参数
ScrapyRT curl 示例:

curl -XPOST -d '{
"spider_name":"quotes",
"start_requests": true,
"param1":"ok"}' "http://localhost:9080/crawl.json"

在你的蜘蛛

def parse(self, response):
    print(self.param1)

此致

相关问题