0

I have a spider that I want to output its results to standard output so that it can be read by subprocess.check_output. I don't want to output to a file as an intermediary.

I've tried adding the flag '-o', 'stdout' but it doesn't work.

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)',
    ])
FLOWMEEN
  • 375
  • 1
  • 3
  • 11

1 Answers1

1

Try this: 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])

Sub yourspider.py

import sys

# your code

print(something what you need to transfer)