Session Variables (Key and Value) in the Web Interface

I'm having trouble getting the session varables to work in the Web Interface. I'm using the following code to initialize the scraping session:

// Loop i with an initial value of APN_START and a maximum value of APN_END.  i++ is increase by one.
for( int i = APN_START; i < APN_END; i++ )

{
   //Add the next value as a session variable
  session.setVariable( "APN", String.valueOf(i) );

// Log
session.log("The current APN: " + session.getVariable("APN"));

// Scrape the next page
session.scrapeFile("04 Search Processing");
}

I set the Session Variables in the Web Interface as Key: APN_START and Value as: 5000
and the second Key: APN_END and second Value as: 6000 but it does not seem to work. I've tried surrounding the APN_START and APN_END with ~# or double quotes but it's still not working. Is there something that is obvious to you that I'm missing?

Thanks!

Adrianjay, You'll need to

Adrianjay,

You'll need to refer to those variables as session variables like so...

for( int i = session.getVariable("APN_START"); i < session.getVariable("APN_END"); i++ )

-Scott

I think you will also need to

I think you will also need to make those variables into Integers.

apnStart = Integer.parseInt(session.getVariable("APN_START"));

for (i=0; i<=apnStart; i++)
{
// All your stuff
}

Good catch Jason.

Good catch Jason.

One more question...

In order to create a different output file for each scraping session I'm trying to use the key value in the web interface as part of the name of the output file. The script for the output file is as follows:


outputFile = "C:/Documents and Settings/My Name/Desktop/Screen Scraper/Database Properties/"Your County" + session.getVariable("APN_START") + ".csv"";

However, I'm receiving the following error message:

An error occurred while processing the script: Write database properties to a file
The error message was: Encountered "Your".

I tried variations for the placement of the double quotes but I'm still receiving an error message. Thanks again for everyones help.

Adrianjay, Can you describe

Adrianjay,

Can you describe what "Your County" is meant to be? Is this a kind of variable? If so, then you would do something like this.

outputFile = "C:/Documents and Settings/My Name/Desktop/Screen Scraper/Database Properties/" + session.getVariable("Your County1") + session.getVariable("APN_START") + ".csv";

Remember, the plus sign "+" is a concatenator tying strings and variables together.

-Scott

Similar Question

Thanks for your quick response to my last question. I'm trying to use the web interface on another scraping session but java seems to not recognize the "session.getVariable("XXX")" of my script. The script is as follows:

import java.io.*;

////////////////////////////////////////////
session.setVariable("INPUT_FILE", "input/session.getVariable("XXX")+.csv");
////////////////////////////////////////////

BufferedReader buffer = new BufferedReader(new FileReader(session.getVariable("INPUT_FILE")));
String line = "";

while ((line = buffer.readLine()) != null){

// The input file in this case will have four items per line
String[] lineParts = line.split(",");

// Set the variables with the parts from the line
session.setVariable("COUNTY", lineParts[0]);
session.setVariable("CITY", lineParts[1]);
session.setVariable("MIN", lineParts[2]);
session.setVariable("MAX", lineParts[3]);

// Output to the log
session.log("***Beginning county cities " + session.getVariable("CITY"));

// Scrape the "Search Results" with the new zip code retrieved from the current state's file
session.scrapeFile("03 So Cal MLS Search Results");

need more quotes

I believe, just looking at it for the first time, that your statement needs more quotes to escape the string characters.

Previous:


session.setVariable("INPUT_FILE", "input/session.getVariable("XXX")+.csv");

now


session.setVariable("INPUT_FILE", "input/" + session.getVariable("XXX")+ ".csv");

as an alternative, you could create that string in a line above like this and then just place it into the session.getVariable.

String myPath = "input/" + session.getVariable("XXX") + ".csv";

session.log("here is the csv path:" + myPath);

session.setVariable("INPUT_FILE", myPath);

hope this helps.