Incrementing session variables

Hi there,

Before I start, I’d just like to say thanks for a fantastic product. I’m currently evaluating SS to see if it can make a planned project viable and having scraped around 11,000 products so far, it is looking extremely promising. Nothing else I’ve tried has come close to being up to the job and it’s great to finally find something that actually does what it says it can!

I’m just after a bit of help with setting some variables in interpreted Java. The search pages I’m scraping are structured using a FromRec= command where each individual page shows 10 results, namely;

http://www.example.com/go.cfm?FromRec=1&tab=&keyword=&search=yes&format=2
http://www.example.com/go.cfm?FromRec=11&tab=&keyword=&search=yes&format=2
http://www.example.com/go.cfm?FromRec=21&tab=&keyword=&search=yes&format=2

So I obviously need to set the session variable to increase by 10 each time aswell. I’ve had a look at the Bean Shell library, but being a non programmer, I’ve not been able to find an answer to what I’m sure is an extremely straightforward question.

Any thoughts would be gratefully received.

Many thanks.

Simon .

Incrementing session variables

Hi Todd,

Many thanks for that, I’ll give it a try along the lines you mention.

Thanks again and all the best,

Simon .

Incrementing session variables

Hi Simon,

If this is the script that invokes your scraping session you'll probably just want to set the initial value. I'd recommend changing it like so:


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

runnableScrapingSession.setVariable( "FromRec", new Integer());

runnableScrapingSession.scrape();

It's more likely that in subsequent scripts you'll want to increment the value. That is, for subsequent search results pages you'll want to increment the record offset, so you might execute a script like the following:

session.log( "FromRec (before increment): " + session.getVariable( "FromRec" ));

session.setVariable
(
"FromRec",
new Integer
(
(( Integer )session.getVariable( "FromRec" )).intValue()
+ 10
)
);

session.log( "FromRec (after increment): " + session.getVariable( "FromRec" ));

So the first URL might look like this:

http://www.example.com/go.cfm?FromRec=1&tab=&keyword=&search=yes&format=2

After that URL is requested you would run the second script, which would make the URL this:

http://www.example.com/go.cfm?FromRec=11&tab=&keyword=&search=yes&format=2

Hopefully that makes sense. If not, please feel free to post back.

Best,

Todd
[email protected]

Incrementing session variables

Hi Todd,

Many thanks for the script, it's greatly appreciated.

I'm sure it's just the way I'm implementing it, but it's giving me the error message copied below. I'm adding it to the script I'm already using to start a scraping session based on the shopping site example in the tutorials.

I've had a play around with it, but to no avail, which I'm sure shows beyond doubt that I really am a total non programmer!

Once again, many thanks for your support and all the best,

Simon.

Script: Test6 start session

Error Message:

BeanShell script error: Sourced file:

session.setVariable( "FromRec", new Integer( 1 ) );

session.log( "FromRec (before increment): " + session.getVariable( "FromRec" ) );

session.setVariable
(
"FromRec",
new Integer
(
( ( Integer )session.getVariable( "FromRec" ) ).intValue()
+ 10
)
);

session.log( "FromRec (after increment): " + session.getVariable( "FromRec" ) );

runnableScrapingSession.scrape();
; > : Attempt to invoke method: setVariable() on undefined variable or class name: session : at Line: 3 : in file:

session.setVariable( "FromRec", new Integer( 1 ) );

session.log( "FromRec (before increment): " + session.getVariable( "FromRec" ) );

session.setVariable
(
"FromRec",
new Integer
(
( ( Integer )session.getVariable( "FromRec" ) ).intValue()
+ 10
)
);

session.log( "FromRec (after increment): " + session.getVariable( "FromRec" ) );

runnableScrapingSession.scrape();
; > : session .setVariable ( "FromRec" , new Integer ( 1 ) ) BSF info: null at line: 0 column: columnNo

Incrementing session variables

That's fantastic, Simon! If you're truly a non-programmer that's great that you've already been so successful with screen-scraper.

As it turns out, incrementing the value isn't as straightforward as it seems like it should be, and it's something that others have asked about, so it's understandable that it would seem confusing. Here's a snippet of code that demonstrates how you would do it using Interpreted Java:

session.setVariable( "FromRec", new Integer( 1 ));

session.log( "FromRec (before increment): " + session.getVariable( "FromRec" ));

session.setVariable
(
"FromRec",
new Integer
(
(( Integer )session.getVariable( "FromRec" )).intValue()
+ 10
)
);

session.log( "FromRec (after increment): " + session.getVariable( "FromRec" ));

The difficulty is that everything must be treated as an object in Java, so it requires a bit of casting to make it work. If it helps at all, we actually have on our to-do list to create a shortcut method for this kind of thing. Watch of it in a future version of screen-scraper.

Using this approach, your URL would then look something like this:

http://www.example.com/go.cfm?FromRec=~#FromRec#~&tab=&keyword=&search=yes&format=2

Hopefully this helps to keep the ball rolling for you. Don't hesitate to post again if we can be of further assistance.

Kind regards,

Todd Wilson
[email protected]