0

I have managed to parse a webpage with powershell and retrieve the text information I need with this code:

$searchClass = "dfx-technicalSentimentCard__signal"
$myURI = "https://www.dailyfx.com/sentiment"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$req = Invoke-Webrequest -URI $myURI
$req.ParsedHtml.getElementsByClassName($searchClass) | %{Write-Host $_.innerhtml}

Now I would like to create a variable containing a number created from the text with Bullish=1, Bearish=2, Mixed=3

Example If the result of my code is:

Mixed
Mixed
Mixed
Mixed
Bullish
Bullish
Bullish
Bearish
Mixed
Bearish
Mixed
Mixed
Bearish
Bearish
Mixed
Bullish
Bullish
Mixed
Bearish
Bullish
Bullish
Mixed
Bullish
Bullish

The number would be 333311123233223113211311

Thank you mklement0 for your solution. Here is the working code:

 $searchClass = "dfx-technicalSentimentCard__signal"
$myURI = "https://www.dailyfx.com/sentiment"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 <# using TLS 1.2 is vitally important #>
$req = Invoke-Webrequest -URI $myURI
$signals = @()
$req.ParsedHtml.getElementsByClassName($searchClass) | %{$signals += $_.innerhtml}
$profile = -join $(switch ($signals) {
    'Mixed'   { 3 }
    'Bullish' { 1 }
    'Bearish' { 2 }
    default   { throw "Unexpected string: $_" }
}) 

Thank you Esperento57 for adding a function, this works and it is a cleaner solution:

function transform([string] $signals)
{
     switch ($signals) {
    'Mixed'   { '3' }
    'Bullish' { '1' }
    'Bearish' { '2' }
    default   { ''  }
    }

}

$searchClass = "dfx-technicalSentimentCard__signal"
$profile=""
$myURI = "https://www.dailyfx.com/sentiment"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$req = Invoke-Webrequest -URI $myURI
$req.ParsedHtml.getElementsByClassName($searchClass) | %{$profile+=transform $_.innerhtml}

$profile
raymonk999
  • 13
  • 2
  • As an aside: [`Write-Host` is typically the wrong tool to use](http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/), unless the intent is to write _to the display only_, bypassing the success output stream and with it the ability to send output to other commands, capture it in a variable, or redirect it to a file. To output a value, use it _by itself_; e.g., `$value` instead of `Write-Host $value` (or use `Write-Output $value`, though that is rarely needed); see [this answer](https://stackoverflow.com/a/60534138/45375) – mklement0 Dec 23 '20 at 23:31

3 Answers3

0

You can use a series of replace statements

$text = @'
Mixed Mixed Mixed Mixed Bullish Bullish Bullish Bearish Mixed Bearish Mixed Mixed Bearish Bearish Mixed Bullish Bullish Mixed Bearish Bullish Bullish Mixed Bullish Bullish
'@

$text -replace '\s' -replace 'Mixed','3' -replace 'Bullish','1' -replace 'Bearish','2'

333311123233223113211311
Doug Maurer
  • 8,090
  • 3
  • 12
  • 13
  • Makes sense with single-line input (+1); unfortunately, the lack of formatting in the original revision of the question disguised that the input is likely an _array_ of elements - that said, your solution should still work. – mklement0 Dec 23 '20 at 23:38
  • I see, but you think it would still work? I'll have to see. Thanks for pointing that out. – Doug Maurer Dec 24 '20 at 00:45
  • Sorry, I misspoke - it wouldn't work as-is, but `(-join $text) -replace 'Mixed','3' ...` would work. – mklement0 Dec 24 '20 at 02:12
0

With open-ended mappings, the most maintainable solution is probably a switch statement:

$strings = 
  'Mixed',
  'Mixed',
  'Mixed',
  'Mixed',
  'Bullish',
  'Bullish',
  'Bullish',
  'Bearish',
  'Mixed',
  'Bearish',
  'Mixed',
  'Mixed',
  'Bearish',
  'Bearish',
  'Mixed',
  'Bullish',
  'Bullish',
  'Mixed',
  'Bearish',
  'Bullish',
  'Bullish',
  'Mixed',
  'Bullish',
  'Bullish'

-join $(switch ($strings) {
    'Mixed'   { 3 }
    'Bullish' { 1 }
    'Bearish' { 2 }
    default   { throw "Unexpected string: $_" }
})

Note the unary form of -join, the string-joining operator, which directly concatenates the (stringified) outputs from the switch statement, passed as an operand to -join via $(), the subexpression operator.

mklement0
  • 382,024
  • 64
  • 607
  • 775
0

Variation with a function :

function transform([string] $signals)
{
     switch ($signals) {
    'Mixed'   { '3' }
    'Bullish' { '1' }
    'Bearish' { '2' }
    default   { ''  }
    }

}

$searchClass = "dfx-technicalSentimentCard__signal"
$profile=""
$myURI = "https://www.dailyfx.com/sentiment"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$req = Invoke-Webrequest -URI $myURI
$req.ParsedHtml.getElementsByClassName($searchClass) | %{$profile+=transform $_.innerhtml}

$profile
Esperento57
  • 16,521
  • 3
  • 39
  • 45