Skip to content

Instantly share code, notes, and snippets.

@pierre3
Last active August 29, 2015 14:10
Show Gist options
  • Save pierre3/d9f567c838e5b536069f to your computer and use it in GitHub Desktop.
Save pierre3/d9f567c838e5b536069f to your computer and use it in GitHub Desktop.
Google検索を行い、検索結果リンクのタイトルとUrlを取得するPowerShell function
<#
.Synopsis
Web検索
.DESCRIPTION
Google検索を行い、検索結果リンクのタイトルとUrlを取得します
.EXAMPLE
$words = 'powerShell', 'script', 'example'
$words | Search-Web
.EXAMPLE
Search-Web powerShell,script,example
#>
function Search-Web
{
[CmdletBinding()]
Param(
# 検索ワード
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
Position=0)]
[PSObject[]]
$SearchWords,
# 取得件数
[int] $num = 10
)
Begin
{
$words = @()
}
Process
{
$words += $SearchWords
}
End
{
$url = "https://www.google.co.jp/search?q={0}&num={1}" -f ($words -join '+'), $num
$res = Invoke-WebRequest -Uri $url
$res.AllElements |
? {$_.tagName -eq "H3" } |
% {[xml]$_.innerHtml } |
? { $_.DocumentElement.href -match "^\/url\?" } |
% { [PSCustomObject]@{
Title = $_.DocumentElement.innerText;
Url = "https://www.google.co.jp$($_.DocumentElement.href)";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment