{"id":327,"date":"2019-10-29T04:26:43","date_gmt":"2019-10-29T04:26:43","guid":{"rendered":"https:\/\/www.heliumscraper.com\/wordpress\/?p=327"},"modified":"2021-09-28T22:07:29","modified_gmt":"2021-09-28T22:07:29","slug":"the-plus-operator","status":"publish","type":"post","link":"https:\/\/www.heliumscraper.com\/blog\/the-plus-operator\/","title":{"rendered":"The Plus Operator"},"content":{"rendered":"\n<p>There are many operators in Helium Scraper, but <a rel=\"noreferrer noopener\" aria-label=\"the plus (+) operator (opens in a new tab)\" href=\"https:\/\/www.heliumscraper.com\/eng\/help\/operators\/sum\/\" target=\"_blank\">the plus (+) operator<\/a> deserves its own tutorial, given the number of uses it has. This is because it doesn&#8217;t just represent addition, but also concatenations of strings and sequences. <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"988\" height=\"300\" src=\"https:\/\/www.heliumscraper.com\/blog\/wp-content\/uploads\/2019\/10\/chaine.jpg\" alt=\"concatenation\" class=\"wp-image-391\" srcset=\"https:\/\/www.heliumscraper.com\/blog\/wp-content\/uploads\/2019\/10\/chaine.jpg 988w, https:\/\/www.heliumscraper.com\/blog\/wp-content\/uploads\/2019\/10\/chaine-300x91.jpg 300w, https:\/\/www.heliumscraper.com\/blog\/wp-content\/uploads\/2019\/10\/chaine-768x233.jpg 768w\" sizes=\"(max-width: 988px) 100vw, 988px\" \/><\/figure>\n\n\n\n<h2>Simple Cases<\/h2>\n\n\n\n<p>Helium Scraper will treat the operator differently, depending on the type of data that is given. For instance, if given numbers, it&#8217;ll behave as a simple addition operator:<\/p>\n\n\n\n<pre class=\"wp-block-code language-hs\"><code>+\n   \u00b7  10.1\n   \u00b7  20.2\n   \u00b7  30.3<\/code><\/pre>\n\n\n\n<p>The above code will produce the number <strong>60.6<\/strong>. <\/p>\n\n\n\n<p>When the operator is given strings, their concatenation is produced:<\/p>\n\n\n\n<pre class=\"wp-block-code language-hs\"><code>+\n   \u00b7  \"Hello \"\n   \u00b7  \"World\"<\/code><\/pre>\n\n\n\n<p>This code will produce <strong>&#8220;Hello World&#8221;<\/strong>. Note that any empty strings are ignored, so the code above is equivalent to this:<\/p>\n\n\n\n<pre class=\"wp-block-code language-hs\"><code>+\n   \u00b7  \"Hello \"\n   \u00b7  \"\"\n   \u00b7  \"World\"<\/code><\/pre>\n\n\n\n<h2>Selectors: Extracting Prices<\/h2>\n\n\n\n<p>Things become interesting when the operator is used with sequences. Suppose you&#8217;re working on a project that extracts from an eCommerce site, where some products have a discount. When they do, the price is shown in a different place and having a completely different style than when there&#8217;s no discount. So it&#8217;s not possible to create a single selector that selects both types of prices, although we&#8217;d like to have either price extracted to the same column. The solution here is to create two selectors for each type of price: <strong>NormalPrice<\/strong> and <strong>DiscountedPrice<\/strong>, and then use the following to extract any of the prices:<\/p>\n\n\n\n<pre class=\"wp-block-code language-hs\"><code>extract\n    price\n       +\n          \u00b7  Select.NormalPrice\n          \u00b7  Select.DiscountedPrice<\/code><\/pre>\n\n\n\n<p>This will work because, just like with strings, empty sequences are ignored. When there&#8217;s no normal price, the concatenation will only contain the discounted price, and when there&#8217;s no discounted price, it&#8217;ll only contain the normal price. Also, in this particular case, if one page happens to contain both prices, only the normal price will be extracted, because only the first element is extracted when the column doesn&#8217;t contain a nested extract action. Finally, if no price is found, the concatenation will be empty, so nothing will be extracted.<\/p>\n\n\n\n<h2>Sequences: Extracting Events<\/h2>\n\n\n\n<p>Not only can selectors be concatenated, but any sequences, even when they contain complex data. Imagine you have a list of URLs for events from which you&#8217;d like to extract information, such as event name, date and address. But some of these URLs are for events that span over multiple dates, and instead of taking you directly to the event details page, they take you to a list of dates, each of which needs to be clicked in order to see the individual event details.<\/p>\n\n\n\n<p>This can be solved by, first, having a selector that selects the event dates, called <strong>EventDate<\/strong>. Also, suppose you have a global called <strong>EventDetails<\/strong> that extracts from each individual event details page. Given this, the following code could be used to extract from both kinds of URLs, and would include every individual date when the event spans over multiple dates:<\/p>\n\n\n\n<pre class=\"wp-block-code language-hs\"><code>Query.EventURLs\nas (url)\nBrowser.Load\n   \u00b7  url\n+\n   \u00b7  Sequence.IfAny\n         \u00b7  Select.EventDate\n         \u00b7  Sequence.Empty\n         \u00b7  EventDetails\n   \u00b7  Select.EventDate\n      Browser.Click\n      EventDetails<\/code><\/pre>\n\n\n\n<p>The way this works is, the top element of the <strong>+<\/strong> operator (<a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/www.heliumscraper.com\/eng\/help\/functions\/sequence\/if_any\/\" target=\"_blank\">Sequence.IfAny<\/a> and its 3 arguments) will either be empty when there are any event dates, or contain the result of <strong>EventDetails<\/strong> when there are no event dates. And the bottom element will contain one <strong>EventDetails<\/strong> for each event date, which implies that it&#8217;ll be empty when there are no event dates. So the result of the operation will be a concatenation of either no items on top and one or more at the bottom (when there are event dates), or a single item on top and no items at the bottom (when there are no event dates).<\/p>\n\n\n\n<p>It is important to note that the data on both elements have the same types. It is not possible to apply the operator to different kinds of data, such as strings and sequences, or sequences containing different types of data.<\/p>\n\n\n\n<p>For more information on this and other operators, see the <a href=\"https:\/\/www.heliumscraper.com\/eng\/help\/operators\/sum\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"plus operator documentation (opens in a new tab)\">plus operator documentation<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are many operators in Helium Scraper, but the plus (+) operator deserves its own tutorial, given the number of uses it has. This is because it doesn&#8217;t just represent addition, but also concatenations of strings and sequences. Simple Cases Helium Scraper will treat the operator differently, depending on the type of data that is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":394,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.heliumscraper.com\/blog\/wp-json\/wp\/v2\/posts\/327"}],"collection":[{"href":"https:\/\/www.heliumscraper.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.heliumscraper.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.heliumscraper.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.heliumscraper.com\/blog\/wp-json\/wp\/v2\/comments?post=327"}],"version-history":[{"count":74,"href":"https:\/\/www.heliumscraper.com\/blog\/wp-json\/wp\/v2\/posts\/327\/revisions"}],"predecessor-version":[{"id":643,"href":"https:\/\/www.heliumscraper.com\/blog\/wp-json\/wp\/v2\/posts\/327\/revisions\/643"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.heliumscraper.com\/blog\/wp-json\/wp\/v2\/media\/394"}],"wp:attachment":[{"href":"https:\/\/www.heliumscraper.com\/blog\/wp-json\/wp\/v2\/media?parent=327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.heliumscraper.com\/blog\/wp-json\/wp\/v2\/categories?post=327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.heliumscraper.com\/blog\/wp-json\/wp\/v2\/tags?post=327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}