如何使scrapy输出到标准输出给Python读

问题描述 投票:0回答:1

我有我要输出其结果到标准输出,以便它可以通过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)',
    ])
python scrapy subprocess
1个回答
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])

子yourspider.py

import sys

# your code

print(something what you need to transfer)
© www.soinside.com 2019 - 2024. All rights reserved.