Сценарий Powershell на Macbook возвращает ошибки - PullRequest
1 голос
/ 23 января 2020

Попытка запустить скрипт PS, который очищает веб-страницу и экспортирует таблицу в файл .csv. Когда я запускаю его на своем Windows рабочем столе, ошибок нет, но когда я пытаюсь запустить его на своем macbook, я получаю следующие ошибки:

InvalidOperation: / users / luke / Downloads / NHLAdvStats.ps1: 7 Line | 7 | $ table = $ data.Parsed Html .getElementsByTagName ("table") | Выберите -ски… | Вы не можете вызвать метод для выражения с нулевым значением.

InvalidOperation: /users/luke/Downloads/NHLAdvStats.ps1:14 Line | 14 | $ headers = $ rows.item (1) .children | select -ExpandProperty InnerText | Вы не можете вызвать метод для выражения с нулевым значением.

InvalidOperation: /users/luke/Downloads/NHLAdvStats.ps1:21 Line | 21 | $ headers = @ ($ headers [0]; 'TEAM'; $ headers [1 .. ($ headers.Length-1)]) | Невозможно индексировать в пустой массив.

InvalidOperation: /users/luke/Downloads/NHLAdvStats.ps1:21 Line | 21 | $ headers = @ ($ headers [0]; 'TEAM'; $ headers [1 .. ($ headers.Length-1)]) | Невозможно индексировать в пустой массив.

InvalidOperation: /Users/luke/Downloads/NHLStats.ps1:9 Line | 9 | $ table = $ data.Parsed Html .getElementsByTagName ("table") | Выберите -ски… | Вы не можете вызвать метод для выражения с нулевым значением.

InvalidOperation: /Users/luke/Downloads/NHLStats.ps1:16 Line | 16 | $ headers = $ rows.item (1) .children | select -ExpandProperty InnerText | Вы не можете вызвать метод для выражения с нулевым значением.

Мой код:

. /users/luke/Downloads/NHLAdvStats.ps1

$url = "https://www.hockey-reference.com/leagues/NHL_2020.html"

#getting the data
$data = Invoke-WebRequest $url

#grab the third table
$table = $data.ParsedHtml.getElementsByTagName("table") | Select -skip 2 | Select -First 1


#get the rows of the Team Statistics table
$rows = $table.rows

#get table headers
$headers = $rows.item(1).children | select -ExpandProperty InnerText
$headers = $headers | % { if ($_ -eq "GF") { "GFPG" }  else { $_ }}

#count the number of rows
$NumOfRows = $rows | Measure-Object

#Manually injecting TEAM header and replace any headers you want to change
$headers = @($headers[0];'TEAM';$headers[1..($headers.Length-1)])

#enumerate the remaining rows (we need to skip the header row) and create a custom object
$allData = @{}
$out = for ($i=2;$i -lt $NumofRows.Count;$i++) {
 #define an empty hashtable
 $objHash=[ordered]@{}
 #getting the child rows
 $rowdata = $rows.item($i).children | select -ExpandProperty InnerText 
 for ($j=0;$j -lt $headers.count;$j++) {
    #add each row of data to the hash table using the correlated table header value
    $objHash[$headers[$j]] = $rowdata[$j]        
  }

  #turn the hashtable into a custom object
  [pscustomobject]$objHash
  $allData.Add($i, $objHash)
}

$out | Select TEAM,AvAge,GP,W,L,OL,PTS,PTS%,GFPG,GA,SOW,SOL,SRS,SOS,TG/G,EVGF,EVGA,PP,PPO,PP%,PPA,PPOA,PK%,SH,SHA,PIM/G,oPIM/G,S,S%,SA,SV%,SO -SkipLast 1 | Export-Csv -Path "/Users/luke/Downloads/$((Get-Date).ToString("'NHL Stats' yyyy-MM-dd")).csv" -NoTypeInformation

Второй скрипт:

$url = "https://www.hockey-reference.com/leagues/NHL_2020.html"

#get the data
$data = Invoke-WebRequest $url

#get the first table
$table = $data.ParsedHtml.getElementsByTagName("table") | Select -skip 3 | Select -First 1


#get the rows
$rows = $table.rows

#get table headers
$headers = $rows.item(1).children | select -ExpandProperty InnerText
$headers = $headers | % { if ($_ -eq "xGF") { "xGFPG" }  else { $_ }}

#count number of rows
$NumOfRows = $rows | Measure-Object

#Manually injecting TEAM header
$headers = @($headers[0];'TEAM';$headers[1..($headers.Length-1)])

#enumerate the remaining rows (we need to skip the header row) and create a custom object
$allData = @{}
$out = for ($i=2;$i -lt $NumofRows.Count;$i++) {
 #define an empty hashtable
 $objHash=[ordered]@{}
 #getting the child rows
 $rowdata = $rows.item($i).children | select -ExpandProperty InnerText 
 for ($j=0;$j -lt $headers.count;$j++) {
    #add each row of data to the hash table using the correlated table header value
    $objHash[$headers[$j]] = $rowdata[$j]        
  }

  #turn the hashtable into a custom object
  [pscustomobject]$objHash
  $allData.Add($i, $objHash)
}

$out | Select TEAM,S%,SV%,PDO,CF,CA,CF%,FF,FA,FF%,xGFPG,xGA,aGF,aGA,axDiff,SCF,SCA,SCF%,HDF,HDA,HDF%,HDGF,HDC%,HDGA,HDCO% | Export-Csv -Path "/Users/luke/downloads/$((Get-Date).ToString("'NHL AdvStats' yyyy-MM-dd")).csv" -NoTypeInformation

Любая и вся помощь с благодарностью Я никогда не сталкивался с этими ошибками раньше, поэтому я немного застрял на том, куда отсюда go.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...