【问题标题】:How to pass parameters to pipelines.py in scrapy?如何在scrapy中将参数传递给pipelines.py?
【发布时间】:2019-07-12 14:51:30
【问题描述】:

我遇到的问题基本上是参数问题。 我有一个有五个蜘蛛的scrapy项目。 蜘蛛必须在由用户输入的坐标分隔的地理区域内提取信息。

此时,区域多边形在 pipelines.py 中定义并被所有蜘蛛使用。 问题是,我应该如何将坐标作为参数从图形界面(tkinter)传递给 pipelines.py?

`class My_Pipeline(object):
    gmaps_key =   googlemaps.Client(key='some_key')
    count = 0
    insidePolygon = 0
    polygon = [(-30.8377119, -64.6603737),
              (-31.5156532, -64.6699867),
              (-31.5507684, -64.4969520),
              (-31.4898937, -64.3349037),
              (-31.3503690, -64.3150073),
              (-31.3052314, -64.3352658),
              (-31.2659561, -64.3623695),
              (-30.9873456, -64.3994484),
              (-30.8412493, -64.4173012),
              (-30.8117674, -64.4763527),
              (-30.8117674, -64.5642433),
              (-30.8377119, -64.6603737)]`

该多边形必须由用户从图形界面输入的坐标替换。

【问题讨论】:

  • 用户输入(图形界面)存储在哪里

标签: python python-3.x scrapy scrapy-pipeline


【解决方案1】:

使用scrapy crawl myspider -a params="1,3,5,6"从命令行传递参数

class MySpider(scrapy.Spider):

    name = 'myspider'

    def __init__(self, 
                 params='can be anything',
                 * args, ** kwargs):

        self.params = params

        super(MySpider, self).__init__()

然后在你的管道中

class My_Pipeline(object):
    spider.params #read if like this

【讨论】:

  • 我仍然在“spider.params”中收到消息“spider 未定义”。
  • 对不起,在管道的process_item方法中使用spider.params
猜你喜欢
  • 2013-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-26
  • 2015-04-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多