2016-12-03 9 views
1

私のコードは以下の通りですが、かなり完成しましたが、PowerShellとInvoke-Webrequestを使用して特定の情報を取得する

基本的にはコードが何をするかは、ソングに基づいて、ある、スクリプトが曲がリリースされたことを歌のラベル、作曲&年を戻すために、インターネット上のウェブサイトのカップルに外に出ます。

だけの事は、特定の曲アリシア・キーズと、です - あなたは手動でWebサイトhttp://staff.australian-charts.com/showitem.asp?interpret=Alicia+Keys+feat%2E+Nicki+Minaj&titel=Girl+On+Fire&cat=sに行けば火 onガールズ、あなたは音楽/歌詞セクションの下にリストされているつ以上の作曲があることがわかります。

曲が複数の作曲家を持っている場合、現在の状態の下の私のスクリプトは、リストされた最初の作曲家のみを掴むでしょう。

私が必要とするのは、スクリプトがすべての作曲家をつかむことです。 1人の作曲があれば、複数の作曲がある場合、または、私は彼らが(コンマを含む)など「Composer1、Composer2、Composer3、Composer4」の形式でキャプチャ必要

私が起動-WebRequestクラスを変更することを考えています....バックテーブルと行など、わからないと、特定のテーブルを取得

$song = "Alicia Keys - Girl On Fire" 
Write-Host $song 
$SearchSong = $song -replace '\(' -replace '\)' -replace '&' -replace ' - ', ' ' -replace '\s','+' 
$MatchSong = $song -replace ' - ', '&titel=' -replace '\s','\+' 

#Check iTunes for music Label information 
$uri = "https://itunes.apple.com/search?term=$SearchSong&country=au&entity=song" 
$x = Invoke-WebRequest -Uri $uri 
$iTunesResults = ($x.Content | ConvertFrom-Json).results 
$y = Invoke-WebRequest -Uri $iTunesResults[0].trackViewUrl 
$iTunesSongCopyright = ($y.ParsedHtml.getElementsByTagName('li') | ? {$_.ClassName -eq 'copyright'}).innerText -replace '℗ ' 
$iTunesSongLabel = $iTunesSongCopyright -replace '.*\d\s' 

#The check australian-charts for Composer & Year infomation 
$domain = 'http://staff.australian-charts.com/' 
$uri = $domain + "search.asp?search=$SearchSong&cat=s" 
$x = Invoke-WebRequest -Uri $uri 

$x.AllElements[462].outerHTML -match 'A.href="(.*)"';$resultURL = $domain + $Matches[1] 
$resultURL = $resultURL -replace("&","&") -replace('"','"') 

$y = Invoke-WebRequest -Uri $resultURL 

$Element = ($y.AllElements | ? {$_.tagName -eq 'HTML'}) 

    if($Element.innerText -match 'Music\/Lyrics:(.*)') 
    { 
     $Element.innerText -match 'Music\/Lyrics:(.*)' 
     $Composer = $Matches[1] 
     Write-Host $Composer 

    } else { 

     $Composer = $null 
    } 

    if($Element.innerText -match 'Year:(.*)') 
    { 
     $Element.innerText -match 'Year:(.*)' 
     $Year = $Matches[1] 
     Write-Host $Year 

    } else { 

     $Year = $null 
    } 

    Write-Host $iTunesSongLabel 
+0

こんにちはマルク、-matchを繰り返す必要はありませんが中かっこの中に - $ matches [1]依然として有効です。 – LotPings

+0

あなたのスクリプトをソースにして$ elements.innertextを見てみると、正規表現で複数行の要素をgrepするには別のアプローチが必要であることがわかります。おそらくhtml dom – LotPings

答えて

1

あなたは作曲家のリスト取得のためにこれを使用することができます。

if($Element.innerText -match 'Music\/Lyrics:(.*)') 
    { 
    $startpos = $Element.innertext.IndexOf("Lyrics:") + 7 
    $endpos = $Element.innertext.IndexOf("Producer:") -1 
    $composer=$Element.innertext.substring($startpos,($endpos - $startpos)) 
    #even the below line will give the same result as the above line if uncommented 
    #$composer = $Element.innertext[$startpos..$endpos] -join "" 
    Write-Host $Composer 

    } 
関連する問題