[英]How to make scrapy output to stdout to be read from Python
我有一个蜘蛛,我想将其结果输出到标准输出,以便可以由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)',
])
试试这个:Main .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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.