Tutorial 4: Page 3: Invoking screen-scraper from Python

Invoking screen-scraper from Python

Before we dig into the code let's review a few things related to invoking screen-scraper via Python. First, your Python code will need to refer to screen-scraper's Python driver, called "remote_scraping_session.py". You can find this file in the "misc\python\" folder of your screen-scraper installation, or you can download it here. You'll want to put a copy of that file into the directory where you plan on putting the Python file that will invoke screen-scraper.

In order to invoke screen-scraper from Python, screen-scraper needs to be running in server mode. If you'd like a refresher on how to start up screen-scraper in server mode go ahead and follow that link, then come back here.

Okay, let's try it out before we go over the code. Download the shopping.py.txt file here, then save it in the same directory where you copied the "remote_scraping_session.py" file. Rename the file from "shopping.py.txt" to "shopping.py". After that start up screen-scraper in server mode.

Run the command "python shopping.py" in your console. You'll be asked which keyword to search. Type in a product keyword, such as "bug", then press "Enter" key. If all goes well the program will take a little while to load (it's waiting as screen-scraper extracts the data), then it will output the corresponding products.

If that didn't go quite as you expected here are some things to check:

  • Make sure screen-scraper is running as a server, and that nothing is blocking its ports (such as a firewall running on your machine).
  • If you're running screen-scraper on a different machine than the one your Python file resides on, make sure that screen-scraper is allowing connections from the Python machine. In the screen-scraper workbench click on the wrench icon, then on the "Servers" button, and check the "Hosts to allow to connect" includes the IP address (or perhaps just the first part of the IP address) of the Python machine.
  • Ensure that the permissions on the "shopping.py" and "remote_scraping_session.py" files are such that you can execute them.
  • Check screen-scraper's "log" folder for a "Shopping Site" log file. If you find one it means that screen-scraper is at least receiving the request. Open the log file in a text editor to see if you find any error messages.
  • If you still can't seem to get it to work feel free to drop us a support request.

Assuming that test worked, fire up your favorite Python editor and open the "shopping.py" file in it. The file is pretty heavily commented, so hopefully it makes sense what's going on. If not, try reviewing Python documentation, posting to our forum, or sending us a support request.

When you invoke screen-scraper as a server it creates log files corresponding to your scraping session in its "log" folder. Take a look in that folder for your "Shopping Site" log file and take a look through it. It should look similar to what you see when you run scraping sessions in the workbench.