Saluts PowerShell-Hoschis
mMn., funktioniert eines meiner TöffForum-PowerShell-Scripte nicht mehr und ich weiss nicht wieso.
Könnte einer von Euch ggf. dieses Dummy-Script unten mal testen?
Eigentlich, will ich nichts anderes, als alle "<article>"-tags (Forum-Posts) aus dem HTML extrahieren. Doch seit dem Forums-Update scheint der HTML-Parser nicht mehr zu funktioneren, wohl weil das HTML nicht mehr "well-formatted" ist (es fehlen einige abschliessende Tags. Item, egal, nehm ich einen RegEx zum Extrahieren der <article>-tags aber nun bin ich auf etwas ganz kurioses gestossen:
Wenn ich einen Web-Request absetze und die Response DIREKT mit RexEx parse ($webRsponse.Content), dann findet der Parser NICHTS! Wenn ich aber die Response zuerst in ein File speichere und dann das File lese, dann findet er die RegEx!
Kurioserweise sind die zu suchenden RegEx-Strings im web response UND in der Datei enthalten.
Kann mir jemand erklären, warum das so ist? ![]()
![]()
![]()
function foobarindeed {
$file = "T:\Temp\Purge Daily\ParseWebPage.html"
$url = "https://www.toeff-forum.ch/thread/13113-e-zigarette-dampfen/?pageNo=1"
# connect to web page
$WebResponse = Invoke-WebRequest $url -UseBasicParsing
if ($WebResponse.StatusCode -eq 200) {
# get web page content
[string]$contentWebResponse = $WebResponse.Content
# save web page to file
Set-Content -Path $file -Value $contentWebResponse
[string]$contentFile = Get-Content $file
# define lookup pattern
$from = "<article "
$to = "</article>"
# define regex pattern
#$pattern = "(?<=<article ).*?(?=</article>)"
#$pattern = '<article .*?</article>'
#Write-Host $pattern
$pattern = "$from.*?$to"
Write-Host "$pattern"
Write-Host "from pattern '$from' exists in web response: $($contentWebResponse.Contains($from))"
Write-Host "to pattern '$to' exists in web response: $($contentWebResponse.Contains($to))"
Write-Host "from pattern '$from' exists in file content: $($contentFile.Contains($from))"
Write-Host "to pattern '$to' exists in file content: $($contentFile.Contains($to))"
# lookup matches in web page content
$matchesWebResponse = ([regex]$pattern).Matches($contentWebResponse) | ForEach-Object { $_.Groups[1].Value }
$matchCountWebResponse = $matchesWebResponse.length
Write-Host "matches in web response content: $matchCountWebResponse"
# lookup matches file content
$matchesFile = ([regex]$pattern).Matches($contentFile) | ForEach-Object { $_.Groups[1].Value }
$matchCountFile = $matchesFile.length
Write-Host "matches in file content: $matchCountFile"
if ($matchCountWebResponse -eq $matchCountFile) {
Write-Host "OK" -ForegroundColor Green
} else {
Write-Host "nope" -ForegroundColor Red
}
} else {
Write-Host "failed to access $url!" -ForegroundColor Red
}
Write-Host "done!"
}