Created
September 11, 2025 10:00
-
-
Save eugrus/9661c2262a8fb239ac92b59bc8784c43 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ( | |
[string]$Query | |
) | |
$apiKey = "" # https://console.cloud.google.com/apis/credentials | |
$cseId = "" # https://programmablesearchengine.google.com/ | |
$results = @() | |
$encodedQuery = [System.Uri]::EscapeDataString($Query) | |
for ($start = 1; $start -le 91; $start += 10) { | |
$url = "https://www.googleapis.com/customsearch/v1?key=$apiKey&cx=$cseId&q=$encodedQuery&num=10&start=$start" | |
Write-Host "Request URL: $url" | |
try { | |
$response = Invoke-RestMethod -Uri $url -Method Get | |
if ($response.items) { $results += $response.items } | |
} catch { | |
Write-Error "Request failed: $($_.Exception.Message)" | |
} | |
Start-Sleep -Milliseconds 200 | |
} | |
$safeQuery = ($Query -replace '[:\\\/\?\*\|"<>\+]', '_') | |
$date = Get-Date -Format "yyyy-MM-dd" | |
$outFile = "{0}_{1}_results.csv" -f $date, $safeQuery | |
$results | Select-Object title, link, snippet | Export-Csv $outFile -NoTypeInformation | |
Write-Output "Saved $($results.Count) results to $outFile" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment