Global Variables

Questions & Answers about Helium Scraper 3
Post Reply
durlecs
Posts: 11
Joined: Sat Sep 28, 2019 2:11 am

Global Variables

Post by durlecs » Sun Sep 29, 2019 7:01 am

How do I access global variables? They seem to get set automatically and then I cannot delete the code because I cannot delete the variable. I'd also like to be able to access and create them so I can use JavaScript like in Helium 2.

There seems to be a lot of functionality that was in Helium 2 that is not in Helium 3. I really want to use Helium 3 because it uses SQLite and I discontinued Helium 3 because it was getting too difficult to use the outdated Access database. But I can't seem to find what I need when writing the JavaScript. Maybe it's all there and I am missing something. Please let me know if Helium 3 has the same functionality as Helium 2 or if it is a cut down version. Thanks.

webmaster
Site Admin
Posts: 491
Joined: Mon Dec 06, 2010 8:39 am
Contact:

Re: Global Variables

Post by webmaster » Sun Sep 29, 2019 6:29 pm

Not sure what you mean by not being able to delete them or by them being set automatically. Global variables in this version are just globals. The previous version only allowed text on global variables, so to emulate this on the new version you'd just create a global and replace the placeholder with any text between double quotes. I typically use an extract action to put a list of variables (you can look at the Setup global on any of our templates for an example).

There is no way to directly retrieve these values from JavaScript. Many of the old JavaScript functions have been removed because there's no need for them anymore, since most of that stuff can be done without JavaScript. What you can do now is pass the values to the script. For instance, if you have a global called Variable1 with this code in it:

Code: Select all

"Hello world"
And then you do this:

Code: Select all

Browser.RunScript
   ·  "global.log(argument);"
   ·  Variable1
It'll print "Hello world" on the log. (Note that instead of using a literal string as a script you can also add the script to Project Explorer -> Scripting -> Scripts and pass it by name with the Script. prefix)

To pass complex data, you can pass objects and lists. For instance, instead of a string, you could put this on Variable1:

Code: Select all

struct
   one
      "Hello world"
   two
      12.34
And then do this:

Code: Select all

Browser.RunScript
   ·  "global.log(argument.one + ' ' + argument.two);"
   ·  Variable1
The values of the struct can also be retrieved without JavaScript like this:

Code: Select all

return
   ·  Variable1
as (one two)
[…]
Here, any action you put on the placeholder and below will have access to the one and two variables.

You may note that the templates I mentioned before use extract instead of struct. This just avoids having to use return as on the code above, but that won't work with Browser.RunScript.
Juan Soldi
The Helium Scraper Team

durlecs
Posts: 11
Joined: Sat Sep 28, 2019 2:11 am

Re: Global Variables

Post by durlecs » Thu Oct 03, 2019 3:17 pm

How would I write to a Global to use it as a variable? I am scraping from multiple tabs and would like all the data to go into one table rather than a table for each tab as is automatically created. Only way I can see doing this is writing the scraped data to a variable and then writing all of the variables to a table at once.

webmaster
Site Admin
Posts: 491
Joined: Mon Dec 06, 2010 8:39 am
Contact:

Re: Global Variables

Post by webmaster » Thu Oct 03, 2019 4:26 pm

You could do something like this:

Code: Select all

extract
   one
      Select.Something
   two
      Select.Something
as (one two)
extract
   three
      Select.Something
   four
      Select.Something
as (three four)
extract
   one
      one
   two
      two
   three
      three
   four
      four
If you right click an extract action and select Output Result it'll output it as variables by appending the "as ..." line.

But I usually wouldn't use this method because it may make the whole extraction slower and the code harder to maintain. What I'd do is leave the extraction as is, and then right click the output table set and select Join Tables to generate a query that shows all tables as a single one. Note that you can also save the query and extract it into an actual table by putting this in a separate global:

Code: Select all

Action.Extract
   ·  Query.MyJoinedTables
   ·  "MySingleTable"
So there's really no need to force the data into a single table during extraction.
Juan Soldi
The Helium Scraper Team

Post Reply