如何使scrapy输出到stdout以从Python读取

时间:2019-02-08 01:31:29

标签: python scrapy subprocess

我有一个Spider,希望将其结果输出到标准输出,以便subprocess.check_output可以读取它。我不想作为中介输出到文件。

我尝试添加标志'-o', 'stdout',但是它不起作用。

test = subprocess.check_output([
        'scrapy', 'runspider', 'spider.py',
        '-a', f"keywords={keywords}", '-a', f'domain={domain}', '-a', f'page={1}',
        '-s', 'USER_AGENT=Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
    ])

1 个答案:

答案 0 :(得分:1)

尝试一下: 主.py

from subprocess import Popen, PIPE

command = ["scrapy runspider yourspider.py -a some additional commands"]
proc = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
proc.wait()
res = proc.communicate()
if proc.returncode:
    print(res[1])
print('result:', res[0])

Sub yourspider.py

import sys

# your code

print(something what you need to transfer)