2016-12-03 49 views
1

我的代碼如下,它非常完整,但是我卡住了一小部分。使用PowerShell和Invoke-Webrequest獲取特定信息

基本上代碼的作用是什麼,根據,腳本出去對互聯網上的一對夫婦的網站帶回歌曲標籤,作曲家&年這首歌被釋放。

唯一的一點是,與特定歌曲艾莉西亞凱斯 - 女消防,如果手動去網站http://staff.australian-charts.com/showitem.asp?interpret=Alicia+Keys+feat%2E+Nicki+Minaj&titel=Girl+On+Fire&cat=s你會發現,有不止一個作曲家音樂/歌詞部分上市。

如果歌曲有多個作曲家,我下面的腳本處於當前狀態,它將獲取僅列出的第一個作曲家。

我需要的是腳本抓住所有的作曲家。如果有一個作曲家,或如果有多個作曲家,我需要他們在「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在大括號內 - $匹配[1]仍然有效。 – LotPings

+0

如果你點出你的腳本來看看$ elements.innertext,你會注意到它需要一個不同的方法來使用正則表達式來處理多行元素,也許與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 

    }