RunnableScrapingSession

Overview

This is a class that can be instantiated within a script in order to run a scraping session.

Also see:

The Maximum number of concurrent running scraping sessions in the settings dialog box will control how many scraping sessions can be run simultaneously.

RunnableScrapingSession

RunnableScrapingSession RunnableScrapingSession ( String name ) (professional and enterprise editions only)
RunnableScrapingSession RunnableScrapingSession ( String name, ScrapingSession inheritedScrapingSession ) (professional and enterprise editions only)
RunnableScrapingSession RunnableScrapingSession ( String name, ScrapingSession inheritedScrapingSession, boolean inheritHttpState ) (professional and enterprise editions only)

Description

Initiates a RunnableScrapingSession object using the name of an existing scraping session.

Parameters

  • name The name of the scraping session to be run, as a string.
  • inheritedScrapingSession (optional) Scraping session whose session variables should be copied to the new scraping session. If it is left off no session variables will be passed to the new scrape.
  • inheritHttpState (optional) Whether HTTP state information, like cookies, should be inherited. This can be important if you have logged into a site and want the runnable scraping sessions to also be logged in.

Return Values

Returns a RunnableScrapingSession. On failure an error will be thrown.

Change Log

Version Description
5.0 inheritHttpState added as optional parameter.
4.5 Available for professional and enterprise editions.

Class Location

com.screenscraper.scraper

Examples

Creating RunnableScrapingSession

 // Creates a new runnable session for the scraping session "My Session".
 myScrapingSession = new com.screenscraper.scraper.RunnableScrapingSession( "My Session" );

 // Creates a new runnable session for the scraping session "My Session"
 // and passes it the current scraping session from which it will inherit
 // session variables and logging.
 myScrapingSession = new com.screenscraper.scraper.RunnableScrapingSession( "My Session", session );

Catching Error

 // If you renamed a scrape and are worried about someone not having the new one
 // you can use the thrown error to identify a problem that can be solved using
 // the older name
 try {
     // Attempt to create scrape using the new name
     myScrapingSession = new com.screenscraper.scraper.RunnableScrapingSession( "My Session - New" );
 } catch ( error ) {
     session.logWarn( error.toString() );
     session.logWarn( "Attemping to start scrape with old name." );
     myScrapingSession = new com.screenscraper.scraper.RunnableScrapingSession( "My Session" );
 }

getName

String runnableScrapingSession.getName ( ) (professional and enterprise editions only)

Description

Retrieve the name of the scraping session in the runnableScrapingSession.

Parameters

This method does not receive any parameters.

Return Values

Returns a string with the name of the scraping session.

Change Log

Version Description
4.5 Available for professional and enterprise editions.

Examples

Retrieve Scrape Name

 runnableScrapingSession = new com.screenscraper.scraper.RunnableScrapingSession( "My Session" );

 // Stores the name of the scraping session in the variable sessionName.
 sessionName = runnableScrapingSession.getName();

getTimeout

int runnableScrapingSession.getTimeout ( ) (professional and enterprise editions only)

Description

Get the timeout of the session in the runnableScrapingSession.

Parameters

This method does not receive any parameters.

Return Values

Returns a integer representing the timeout length in minutes.

Change Log

Version Description
4.5 Available for professional and enterprise editions.

Examples

Write Timeout to Log

 runnableScrapingSession = new com.screenscraper.scraper.RunnableScrapingSession( "My Session" );

 // Outputs the value of the timeout of the runnable scraping session
 // to the log.
 session.log( "Session timeout: " + runnableScrapingSession.getTimeout() );

See Also

getVariable

Object runnableScrapingSession.getVariable ( String variableName ) (professional and enterprise editions only)

Description

Retrieve the the value of a session variable. This method should be called after scrape method has returned.

Parameters

  • variableName Name of the variable, as a string.

Return Values

Returns the value of the session variable: object, boolean, int, string, etc. If the variable doesn't exists it returns null.

Change Log

Version Description
4.5 Available for professional and enterprise editions.

Examples

Write Variable to Log

 runnableScrapingSession = new com.screenscraper.scraper.RunnableScrapingSession( "My Session" );

 // Ensure scrape will be run before the script continues
 runnableScrapingSession.setDoLazyScrape( false );

 // Start the scrape
 runnableScrapingSession.scrape();

 // Outputs the value of the variable FOO to the log.
 session.log( "FOO: " + runnableScrapingSession.getVariable( "FOO" ) );

See Also

  • setVariable() [RunnableScrapingSession] - Sets a variable for the scraping session

scrape

void runnableScrapingSession.scrape() (professional and enterprise editions only)

Description

Run the session scraping.

Parameters

This method does not receive any parameters.

Return Values

Returns void.

Change Log

Version Description
4.5 Available for professional and enterprise editions.

The default is for the script to continue executing without waiting for the scraping session to finish. You can use setDoLazyScrape to force the script to wait until the scape finishes before continuing the script.

Examples

Start Scrape in Separate Thread

 runnableScrapingSession = new com.screenscraper.scraper.RunnableScrapingSession( "My Session" );

 // Tells the session to start scraping.
 runnableScrapingSession.scrape();

 // Script continues execution without waiting for end of scrape

Start Scrape in Same Thread

 runnableScrapingSession = new com.screenscraper.scraper.RunnableScrapingSession( "My Session" );

 // Turn off LazyScrape
 runnableScrapingSession.setDoLazyScrape( false );

 // Tells the session to start scraping.
 runnableScrapingSession.scrape();

 // Script halts execution until the scrape is finished

setDoLazyScrape

void runnableScrapingSession.setDoLazyScrape ( boolean doLazyScrape ) (professional and enterprise editions only)

Description

Indicate whether or not the scraping session should run concurrently with (at the same time as) other scraping sessions. The default for doLazyScrape is true.

Parameters

  • doLazyScrape If lazy (concurrent) scraping should be used, as a boolean.

Return Values

Returns void.

Change Log

Version Description
4.5 Available for professional and enterprise editions.

We recommend not setting this value to false! When running scraping sessions in the workbench, it will cause the interface to freeze up until sessions have completed.

If you'd like to run multiple scraping sessions serially (one after another), the best option is to set the Maximum number of concurrent running scraping sessions to 1 in the settings window.

Examples

Turn off LazyScrape

 runnableScrapingSession = new com.screenscraper.scraper.RunnableScrapingSession( "My Session" );

 // Indicates that the runnable scraping session should not be run
 // in a separate thread.
 runnableScrapingSession.setDoLazyScrape( false );

 // Start the scrape
 runnableScrapingSession.scrape();

setTimeout

void runnableScrapingSession.setTimeout ( int timeout ) (professional and enterprise editions only)

Description

Sets the timeout of the session. That is, after the given number of minutes have passed the session will automatically terminate.

Parameters

  • timeout An integer representing the timeout length in minutes.

Return Values

Returns void.

Change Log

Version Description
4.5 Available for professional and enterprise editions.

This method must be called before scrape.

Examples

Set Scrape Timeout

 runnableScrapingSession = new com.screenscraper.scraper.RunnableScrapingSession( "My Session" );

 // Sets the timeout of the session to 60 minutes.
 runnableScrapingSession.setTimeout( 60 );

 runnableScrapingSession.scrape();

See Also

  • getTimeout() [RunnableScrapingSession] - Retrieves the timeout for the session

setVariable

void runnableScrapingSession.setVariable ( String identifier, Object value ) (professional and enterprise editions only)

Description

Set the value of a session variable.

Parameters

  • identifier Name of the variable, as a string.
  • value What to store in the variable: object, boolean, int, string, etc.

Return Values

Returns void.

Change Log

Version Description
4.5 Available for professional and enterprise editions.

Examples

Set Session Variable

 runnableScrapingSession = new com.screenscraper.scraper.RunnableScrapingSession( "My Session" );

 // Sets the session variable "LOGIN_USERNAME" with the value
 // "my_username".
 runnableScrapingSession.setVariable( "LOGIN_USERNAME", "my_username" );

 // Start the scrape
 runnableScrapingSession.scrape();

See Also

  • getVariable() [RunnableScrapingSession] - Retrieves the value of a session variable from the scraping session