Python Scrapy:将参数传递给cmdline.execute()
我知道在从命令行运行scrapy spider时如何传递参数。但是,我在尝试使用scrapy的cmdline.execute()从脚本以编程方式运行它时遇到了问题 我需要传递的参数是我以前格式化为字符串的列表,如下所示:Python Scrapy:将参数传递给cmdline.execute(),python,web-scraping,scrapy,Python,Web Scraping,Scrapy,我知道在从命令行运行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 ar
numbers = "one,two,three,four,five"
colors = "red,blue,black,yellow,pink"
cmdline.execute('scrapy crawl myspider -a arg1='+numbers+' -a arg2='+colors)
而蜘蛛是
class MySpider(Spider):
name = "myS"
def __init__(self, arg1, arg2):
super(MySpider, self).__init__()
#Rest of the code
但是,当我运行它时,会出现以下错误:
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
有什么想法吗
操作系统:Windows7;
Python版本:2.7.8函数要求的是参数列表,而不是字符串。试试这个:
cmdline.execute([
'scrapy', 'crawl', 'myspider',
'-a', 'arg1='+numbers, '-a', 'arg2='+colors])
是否缺少.split()?试试下面的方法,看看会发生什么
cmdline.execute("scrapy crawl myspider -a arg1="+numbers+" -a arg2=" + colors + "".split())
对不起,这对我不起作用。它返回“TypeError:无法连接'str'和'list'对象”,我面临同样的问题,对我来说,这是因为混淆了引号和加号。修好它之后,它工作了