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