2017-11-18 9 views
1

CSV 또는 JSON 형식의 국가에 대한 ISO 코드를 검색하려고합니다. 내 코드는 다음과 같습니다 :웹 사이트에서 국가 ISO 코드 받기

# ############################ 
$logFile = "$env:USERPROFILE\desktop\ISOCountry.log" 
Start-Transcript -Path $logFile -Append 
######################################### 

$WebResponse = Invoke-WebRequest "http://kirste.userpage.fu-berlin.de/diverse/doc/ISO_3166.html" 
#$WebResponse = Invoke-WebRequest "https://en.wikipedia.org/wiki/ISO_3166-1" 
$PRETAG = $WebResponse.ParsedHtml.getElementsByTagName("PRE") | select -expand innertext 
$PRETAG 
$JsonText = $PRETAG | ConvertTo-csv 
$JsonText 
# end logging 
########################### 
Stop-Transcript 
########################### 

데이터는 PRE 태그 내에이며, 탭으로 구분 된 형식으로 모든 아마. 이것에 대한 도움이 필요합니다. 이 사이트는 무료 사이트이므로이 사이트를 사용하고 있습니다.

내가 위키에서 데이터를 검색하고 아래 코드를 사용하여 동일한을 검색 할 수 없습니다 시도 :

$URI = “https://en.wikipedia.org/wiki/ISO_3166-1“ 
$HTML = Invoke-WebRequest -Uri $URI 
($HTML.ParsedHtml.getElementsByTagName('table') | Where{ $_.className -eq 'wikitable sortable' }).innerText 

여전히 같은 문제에 직면. 도움이 필요하다.

답변

1

이것은 내가 HTMLAgilityPack과 관련이 있습니다. 팩을 http://html-agility-pack.net/ 에서 다운로드 할 수 있습니다. XPath와 함께 웹 사이트를 긁어 모으기 위해 널리 사용되는 프레임 워크입니다.

"country";"iso" 
"Afghanistan";"ISO 3166-2:AF" 
"Aland Islands !Åland Islands";"ISO 3166-2:AX" 
"Albania";"ISO 3166-2:AL" 
"Algeria";"ISO 3166-2:DZ" 
"American Samoa";"ISO 3166-2:AS" 

편집 :이 당신과 같은 출력을 줄 것

cls 
[void][Reflection.Assembly]::LoadFile("C:\temp\HtmlAgilityPack\lib\Net20\HtmlAgilityPack.dll”) 
[HtmlAgilityPack.HtmlWeb]$web = @{} 
[HtmlAgilityPack.HtmlDocument]$doc = $web.Load("https://en.wikipedia.org/wiki/ISO_3166-1") 

## FILTER NEEDED CONTENT THROUGH X-PATH 
[HtmlAgilityPack.HtmlNodeCollection]$country = $doc.DocumentNode.SelectNodes("//table[2]//tr//td[1]") 
[HtmlAgilityPack.HtmlNodeCollection]$iso = $doc.DocumentNode.SelectNodes("//table[2]//tr//td[5]") 

# go trough the arrays and put each item into output 
$output = @() 
for($i=0; $i -le $country.selectnodes.Count; $i++){ 

    $output += [pscustomobject] @{ 
    country = $country[$i].InnerText 
    iso = $iso[$i].innertext 
    }  
} 
# export csv 
$output | ConvertTo-Csv -Delimiter ";" -NoTypeInformation | out-file C:\temp\iso.csv -Force 

더 성능이 좋은 방법에게 HTMLAgility 모듈에 대한

0

감사를 발견했다. 나는 ADMIN에게 모듈을 설치하는 권한이없는 그래서 이것은 내가 그것을 어떻게 있습니다 :

<################################################CODE HEADER############################################ 
SCRIPT NAME  : ISO Country Code.ps1 
DESCRIPTION  : 
RUNTIME PARAMETERS: 
INPUT PARAMETERS : 
OUTPUT PARAMETERS : 
Date              Developer         Description 
-------------------------------------- ---------------------------------------------- --------------------------------- 

################################################CODE HEADER############################################> 

<################################################SAMPLE DATA############################################ 
name   : Zimbabwe 
topLevelDomain : {.zw} 
alpha2Code  : ZW 
alpha3Code  : ZWE 
callingCodes : {263} 
capital  : Harare 
altSpellings : {ZW, Republic of Zimbabwe} 
region   : Africa 
subregion  : Eastern Africa 
population  : 14240168 
latlng   : {-20.0, 30.0} 
demonym  : Zimbabwean 
area   : 390757.0 
gini   : 
timezones  : {UTC+02:00} 
borders  : {BWA, MOZ, ZAF, ZMB} 
nativeName  : Zimbabwe 
numericCode : 716 
currencies  : {@{code=BWP; name=Botswana pula; symbol=P}, @{code=GBP; name=British pound; symbol=£}, @{code=CNY; name=Chinese yuan; symbol=¥}, @{code=EUR; name=Euro; 
       symbol=€}...} 
languages  : {@{iso639_1=en; iso639_2=eng; name=English; nativeName=English}, @{iso639_1=sn; iso639_2=sna; name=Shona; nativeName=chiShona}, @{iso639_1=nd; iso639_2=nde; 
       name=Northern Ndebele; nativeName=isiNdebele}} 
translations : @{de=Simbabwe; es=Zimbabue; fr=Zimbabwe; ja=ジンバブエ; it=Zimbabwe; br=Zimbabwe; pt=Zimbabué; nl=Zimbabwe; hr=Zimbabve; fa=زیمباوه} 
flag   : https://restcountries.eu/data/zwe.svg 
regionalBlocs : {@{acronym=AU; name=African Union; otherAcronyms=System.Object[]; otherNames=System.Object[]}} 
cioc   : ZIM 
###############################################################################################################################> 

# ############################ 
$logFile = 'D:\03_PowerShell\PowerShell Scripts\ISO Country Code\ISOCountryCodes.log' 
Start-Transcript -Path $logFile -Append 

$isodate = Get-Date -Format ddMMMyyyy_HHhmmss 

#BUILDING FILE PATH 
$FilePath = "D:\03_PowerShell\PowerShell Scripts\ISO Country Code\" 

#BUILDING JSON FILE PATH 
$JsonFileName = "ISOCountryCode_$isodate" 
$JsonFileExtn = ".json" 
$JsonFileOutputPath = $FilePath+$JsonFileName+$JsonFileExtn 

#BUILDING CSV FILE PATH 
$CsvFileName = "ISOCountryCode_$isodate" 
$CsvFileExtn = ".csv" 

#CSV File Path 
$CsvFileOutputPath = $FilePath+$CsvFileName+$CsvFileExtn 

"The Log File is placed at: - $logFile" 
"The ISO Date is: - $isodate " 
"The Full File Path for the JSON File is: - $JsonFileOutputPath" 
"The Full File Path for the CSV File is: - $CsvFileOutputPath" 

#CLEAR RESULT SCREEN 
cls 

#INVOKE REST METHOD TO RETREIEVE DATA 
$ISOCountryCode = Invoke-RestMethod "https://restcountries.eu/rest/v2/all" 
$ISOCountryCodeFormatted = $ISOCountryCode | Select-Object @{Name="Country Name";Expression={$_."name"}} ` 
                  ,@{Name = "Internet Domain"; Expression={$_."topLevelDomain"}} ` 
                  ,@{Name = "Alpha 2 Code"; Expression={$_."alpha2Code"}} ` 
                  ,@{Name = "Alpha 3 Code"; Expression={$_."alpha3Code"}} ` 
                  ,@{Name = "Capital"; Expression={$_."capital"}} ` 
                  ,@{Name = "Continent"; Expression={$_."region"}} ` 
                  ,@{Name = "Area (LandMass)"; Expression={$_."area"}} ` 
                  ,@{Name = "Numeric Code"; Expression={$_."numericCode"}} ` 

<#BELOW COLUMNS AND VALUES TO BE USED AS REQUIRED 
#,@{Name = "population"; Expression={$_."population"}} ` 
#,@{Name = "Languages Used"; Expression={$_."languages "}} ` 
#,@{Name = "International Dialling Code"; Expression={$_."callingCodes "}} ` 
#,@{Name = "Latitude/Longitude"; Expression={$_."latlng"}} ` 
#,@{Name = "Translations"; Expression={$_."translations"}} 
#,@{Name = "Country Direction/Location"; Expression={$_."subregion "}} ` 
#,@{Name = "Timezones"; Expression={$_."timezones "}} ` 
#,@{Name = "Borders"; Expression={$_."borders "}}#> ` 

#TEST THE CODE TO CONVERT RESULTSET TO JSON 
#$JsonText = $Foo1 | ConvertTo-Json 
#$JsonText 

$ISOCountryCodeFormatted | ft 

#BUILD A JSON FILE 
$text | Set-Content $JsonFileOutputPath 

#BUILD JSON FILE WITH HEADER > DATA > FOOTER 
$JsonHeader = '{ 
    "ISOCountryCodes":' | Add-Content $JsonFileOutputPath 
$ISOCountryCodeFormatted | ConvertTo-Json | Add-Content $JsonFileOutputPath 
$JsonFooter = '}' | Add-Content $JsonFileOutputPath 

#GENERATE CSV FILE 
$ISOCountryCodeFormatted |Export-Csv $CsvFileOutputPath -encoding "unicode" -NoTypeInformation 

#END TRANSCRIPT FOR LOGGING DATA FLOW 
Stop-Transcript 
+0

내가, 당신이 HTMLAgilityPack 다운로드 만 할 수 말할 압축을 해제하고 바탕 화면에 배치 할 죄송합니다. 설치가 필요 없습니다. 그것이이 팩의 아름다움입니다. 그러나 당신이 당신 자신의 대본을 만든 것이 좋다. – Snak3d0c