Advertisement
Namokonov

parse_WB

Jan 31st, 2023
1,114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function onOpen() {
  2.   SpreadsheetApp.getUi().createMenu("🔥")
  3.     .addItem('Загрузить!', 'myFunction')
  4.     .addToUi();
  5. };
  6.  
  7. function myFunction() {
  8.   const url = 'https://card.wb.ru/cards/detail?nm='
  9.   const ss = SpreadsheetApp.getActive()
  10.   const nms = ss.getSheetByName('Что парсим').getDataRange().getValues().filter(d => d[1]).map(d => d[0]).join(";");
  11.   const shTo = ss.getSheetByName('Загруженное');
  12.   var response = UrlFetchApp.fetch(url + nms);
  13.   response = JSON.parse(response);
  14.   var arr = parse(response);
  15.   shTo.clear();
  16.   shTo.getRange(1, 1, arr.length, arr[0].length).setValues(arr);
  17. };
  18.  
  19. function parse(json) {
  20.   const r = json['data']['products'];
  21.   var arr = [];
  22.   for (n in r) {
  23.     const fl = r[n];
  24.     const id = fl?.['id'];
  25.     const name = fl?.['name'];
  26.     const brand = fl?.['brand'];
  27.     const brandId = fl?.['brandId'];
  28.     const priceU = fl?.['priceU'] / 1000;
  29.     const sale_prc = fl?.['sale'];
  30.     const salePriceU = fl?.['salePriceU'] / 1000;
  31.     const pics = fl?.['pics'];
  32.     const rating = fl?.['rating'];
  33.     const feedbacks = fl?.['feedbacks'];
  34.     const volume = fl?.['volume'];
  35.     const colors = fl?.['colors'].map(t => t['name']).join(', ');
  36.     const sizes = fl?.['sizes'];
  37.  
  38.     for (nn in sizes) {
  39.       const size = sizes[nn];
  40.       const size_name = size['name'];
  41.       const size_origName = size['origName'];
  42.       const stocks = JSON.stringify(size['stocks']);
  43.  
  44.       arr = arr.concat([[id, name, brand, brandId, priceU, sale_prc, salePriceU, pics, rating, feedbacks, volume, colors, size_name, size_origName, stocks]]);
  45.     };
  46.   };
  47.  
  48.   return [['id', 'name', 'brand', 'brandId', 'priceU', 'sale_prc', 'salePriceU', 'pics', 'rating', 'feedbacks', 'volume', 'colors', 'size_name', 'size_origName', 'stocks']]
  49.     .concat(arr);
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement