-1

I have a .exe programme that produces real-time data. I want to extract the output when running the programme in real-time, however It's my first time trying this out, and so I wanted help in approaching this.

I have opened it with the following:

cmd = r'/Applications/StockSpy Realtime Stocks Quote.app/Contents/MacOS/StockSpy Realtime Stocks Quote'

import subprocess

with open('output.txt', 'wb') as f:
    subprocess.check_call(cmd, stdout=f)

# to read line by line
with open('output.txt') as f:
    for line in f:
        print(line)
# output = qx(cmd)

with the aim to store the output. However, it does not save any of the output, I get a blank textfile.

I managed to save the output by following this code:

from subprocess import STDOUT, check_call as x

with open(os.devnull, 'rb') as DEVNULL, open('output.txt', 'wb') as f:
    x(cmd, stdin=DEVNULL, stdout=f, stderr=STDOUT)

from How do I get all of the output from my .exe using subprocess and Popen?

Working dollar
  • 306
  • 2
  • 10
  • Can you specify what information do you want to scrape? – Md. Fazlul Hoque May 12 '22 at 17:33
  • @F.Hoque For this specific case, I wanted to scrape the stock prices from the app linked in the post. Otherwise, I wanted to know whether scrapy has the capability to scrape apple store downloaded apps, or if python libraries exist for this. I know that websites have an html structure, and so the app may not permit scraping through this, but surely it holds data in the back-end, perhaps this is accessible? – Working dollar May 12 '22 at 17:57
  • Are you asking if you can get scrapy to work on an iphone? – Alexander May 12 '22 at 19:04
  • if those apps you mentioned have the same information on the web, then scrapy can get it... – Alexander May 12 '22 at 19:05
  • @alexpdev they do not share the info on the web only in the app. The app is built with `c`, and they have an executable that grabs the real-time data from their database with websocket. I have changed my question, so it addresses a specific focus on how to get real-time data from the executable. – Working dollar May 12 '22 at 19:10
  • What you're doing does not RUN the app. It just opens the executable as a data file. What you need, I believe, is `subprocess.Popen`, so you can run the application and read the output that it prints. – Tim Roberts May 12 '22 at 19:12
  • @TimRoberts Thank you for the suggestion, I briefly looked into this before but wasn't sure this was what I needed. I will dig into it some more. On a additional note; could this run the programme, extract and store the data in real-time, say if I got the data and connected it to an sql database, without stopping (unless I quit it) – Working dollar May 12 '22 at 19:13
  • Sure, that's just coding. HOWEVER, whatever they're doing with a Websocket, you could do inside your own app. You don't really need the application. – Tim Roberts May 12 '22 at 19:21

1 Answers1

0

What you are trying to do can be achieved with python using something like this:

import subprocess
with subprocess.Popen(['/path/to/executable'], stdout=subprocess.PIPE) as proc:
    data = proc.stdout.read()   # the data variable will contain the 
                                # what would usually be the output 
    """Do something with data..."""
Alexander
  • 16,091
  • 5
  • 13
  • 29