Defining parameters using variables

Hi,

I have a page which requires startdate, enddate parameters. Currently they are hardcoded, but I want to define variables for them and call them from a script as:

runnableScrapingSession.setVariable( "START_DATE", yesterdayDate );
runnableScrapingSession.setVariable( "END_DATE", yesterdayDate );

What do I need to do?

Thanks in advance,

Mark V.

Defining parameters using variables

Fabulous! Feel free to post again in the future if we can help with anything else.

Best wishes,

Todd

Defining parameters using variables

Thanks! That worked, the problem was my variable naming and the fact that I was passing a date instead of a formatted string,

Defining parameters using variables

Hi Mark,

It looks as though you just need to use consistent names for the variables. In your POST parameter if you use the token ~#START_DATE#~ for the value, then the line used to set that session variable in your script should look like this:


runnableScrapingSession.setVariable( "START_DATE", today );

Also, and you may already be aware of this, but you may need to format the date prior to setting it as a session variable. The Java Date class may not return quite what you're expecting if you simply pass along the raw object. For example, you may want to do your script like this:


import java.text.SimpleDateFormat;

Date today = new Date();
SimpleDateFormat formatter = new SimpleDateFormat( "MM/dd/yyyy" );
String formattedDate = formatter.format( today );

runnableScrapingSession = new com.screenscraper.scraper.RunnableScrapingSession( "TCPL" );
runnableScrapingSession.setVariable( "START_DATE", formattedDate );
runnableScrapingSession.setVariable( "END_DATE", formattedDate );
runnableScrapingSession.scrape();

You can read more about how to use the SimpleDateFormat class here: http://java.sun.com/j2se/1.5.0/docs/api/java/text/SimpleDateFormat.html .

Kind regards,

Todd

Post Parameters

Hi Todd,

Thanks, but I'm still having trouble. Ok, I have two
Post Parameters:
Key1: StartDate Value1: ~#START_DATE#~
Key2: EndDate Value2: ~#END_DATE#~

I also have a script which I use to run my scraping session as:

Date today = new Date();

runnableScrapingSession = new com.screenscraper.scraper.RunnableScrapingSession( "TCPL" );
runnableScrapingSession.setVariable( "StartDate", today );
runnableScrapingSession.setVariable( "EndDate", today );
runnableScrapingSession.scrape();

However, in the log I noticed that nothing was getting passed in for these parameters and I get no data back.

Mark

Defining parameters using variables

Hi Mark,

Thanks for the posting. It sounds as though what you'd like to do is similar to what's illustrated in our second tutorial here http//www.screen-scraper.com/support/tutorials/tutorial2/embedding_session_variables.php . However, it may be that you're wanting to embed the session variable values in POST parameters rather than in the URL. If such is the case you simply need use the same type of tokens shown in that tutorial page I referred to in your POST parameter. For example, you might have a scrapeable file that uses POST parameters called "start_date" and "end_date", which would be found under the "Parameters" tab for that scrapeable file. Right now it sounds like you might have something like "01/26/06" hard-coded for the "start_date" parameter. In order to use the dynamic values you're setting in your script you can simply substitute ~#START_DATE#~ for the hard-coded "01/26/06" value, and screen-scraper will dynamically insert it in when the scraping session runs.

Hopefully this answers your question. If not, feel free to write back so that I can clarify.

Kind regards,

Todd Wilson