小编pyl*_*und的帖子

Scrapy:将参数传递给cmdline.execute()

我知道如何在命令行中运行scrapy spider时传递参数.但是,当我尝试使用scrapy的cmdline.execute()以编程方式运行它时,我遇到了问题.

我需要传递的参数是我之前格式化为字符串的列表,就像这样:

numbers = "one,two,three,four,five"
colors = "red,blue,black,yellow,pink"

cmdline.execute('scrapy crawl myspider -a arg1='+numbers+' -a arg2='+colors)
Run Code Online (Sandbox Code Playgroud)

蜘蛛是......

    class MySpider(Spider):

        name = "myS"

        def __init__(self, arg1, arg2):
            super(MySpider, self).__init__()

#Rest of the code
Run Code Online (Sandbox Code Playgroud)

但是,当我运行它时,我收到此错误:

  Traceback (most recent call last):
  File "C:/Users/ME/projects/script.py", line 207, in run
    cmdline.execute("scrapy crawl myS -a arg1="+numbers+" -a data="+colors)
  File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 123, in execute
    cmdname = _pop_command_name(argv)
  File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 57, in _pop_command_name
    del argv[i]
TypeError: 'str' object doesn't support item deletion
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

操作系统:Windows7; Python版本:2.7.8

python scrapy web-scraping

6
推荐指数
2
解决办法
2042
查看次数

标签 统计

python ×1

scrapy ×1

web-scraping ×1