Last active
August 9, 2024 05:17
-
-
Save JinKwonJeon/34e5536b6bf13cb90d4a7eab4b368a18 to your computer and use it in GitHub Desktop.
NaverDataLab_Age 함수 (PowerQuery M)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (Keywords as text, optional startDate as text, optional endDate as text, optional ClientID as text, optional ClientSecret as text) => | |
| let | |
| // Step 1: 시작 날짜 및 종료 날짜 설정 (필수 입력값이 아닌 경우 기본값으로 설정) | |
| sDate = if (startDate is null) then Text.From(DateTime.Date(Excel.CurrentWorkbook(){[Name="tblNaver"]}[Content][StartDate]{0})) else startDate, | |
| eDate = if (endDate is null) then Text.From(DateTime.Date(Excel.CurrentWorkbook(){[Name="tblNaver"]}[Content][EndDate]{0})) else endDate, | |
| // Step 2: 클라이언트 ID 및 시크릿 설정 (필수 입력값이 아닌 경우 기본값으로 설정) | |
| cID = if (ClientID is null) then Text.From(Excel.CurrentWorkbook(){[Name="tblNaver"]}[Content][ClientID]{0}) else ClientID, | |
| cSecret = if (ClientSecret is null) then Text.From(Excel.CurrentWorkbook(){[Name="tblNaver"]}[Content][ClientSecret]{0}) else ClientSecret, | |
| // Step 3: 네이버 쇼핑 메인 페이지에서 카테고리 값 추출 | |
| NShoppingMain = Web.BrowserContents("https://search.shopping.naver.com/search/all?query="&Keywords), | |
| CatPos = Text.PositionOf(NShoppingMain, "mainFilters"), | |
| TextAfterMainFilter = Text.Middle(NShoppingMain, CatPos, Text.Length(NShoppingMain) - CatPos), | |
| ValuePos = Text.PositionOf(TextAfterMainFilter, "value"":"""), | |
| CatValue = Text.Middle(TextAfterMainFilter, ValuePos + 8, 8), | |
| // Step 4: 네이버 데이터랩 쇼핑 API 요청을 위한 URL 및 본문 생성 | |
| url = "https://openapi.naver.com/v1/datalab/shopping/category/keyword/age", | |
| body = "{ | |
| ""startDate"": """ & sDate & """, | |
| ""endDate"": """ & eDate & """, | |
| ""timeUnit"": ""date"", | |
| ""category"": """ & CatValue & """, | |
| ""keyword"": """ & Keywords & """ | |
| }", | |
| // Step 5: 요청 헤더 설정 | |
| headers = [ | |
| #"Content-Type"="application/json", | |
| #"X-Naver-Client-Id"= cID, | |
| #"X-Naver-Client-Secret"=cSecret | |
| ], | |
| // Step 6: API 요청 보내기 및 응답 데이터 처리 | |
| Source = Json.Document(Web.Contents(url, [Content=Text.ToBinary(body), Headers=headers, ManualStatusHandling={400, 404, 500}])), | |
| results = Source[results], | |
| // Step 7: 결과를 테이블 형식으로 변환 및 필요한 열 확장 | |
| ListToTable = Table.FromList(results, Splitter.SplitByNothing(), null, null, ExtraValues.Error), | |
| ColName = List.First(Table.ColumnNames(ListToTable)), | |
| FieldNames = Record.FieldNames(Record.Field(ListToTable{0}, ColName)), | |
| ExpandTable = Table.ExpandListColumn(Table.ExpandListColumn(Table.ExpandRecordColumn(ListToTable, ColName, FieldNames, FieldNames), "data"), "keyword"), | |
| ExpandRecords = Table.RemoveColumns(Table.ExpandRecordColumn(ExpandTable, "data", {"period", "group","ratio"}, {"period", "group", "ratio"}),{"title"}), | |
| // Step 8: 나이 그룹을 한글로 변환 | |
| ReplaceList = [#"10" = "10대", #"20" = "20대", #"30" = "30대", #"40" = "40대", #"50" = "50대", #"60" = "60대"], | |
| NaverDataLab_Age = Table.TransformColumns(ExpandRecords,{{"group",each Record.FieldOrDefault(ReplaceList,_,_)}}) | |
| in | |
| NaverDataLab_Age |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2024.08.09 : 파워쿼리 Web 호출 기능 업데이트로 인한 코드 수정
Web.Contents -> Web.BrowserContent