Parsh something like this HTML using BeautifulSoup:

[<div class="spec"> <div class="spec_title" style="width: 115px;"><span>Артикул</span></div> <div class="spec_value" style="width: 165px;"><span>LED-LP-100M-15CM-12V-F</span></div> </div>, <div class="spec"> <div class="spec_title"><span>Тип светодиода</span></div> <div class="spec_value"><span>DIP 5 мм</span></div> </div>, <div class="spec"> <div class="spec_title"><span>Кол-во светодиодов</span></div> <div class="spec_value"><span>666 шт</span></div> </div>, <div class="spec"> <div class="spec_title"><span>Кол-во мерцающих светодиодов</span></div> <div class="spec_value"><span>100 шт</span></div> </div>, <div class="spec"> <div class="spec_title"><span>Шаг</span></div> <div class="spec_value"><span>15 см</span></div> </div>, <div class="spec"> <div class="spec_title"><span>Рабочее напряжение</span></div> <div class="spec_value"><span>12 вольт</span></div> </div>, <div class="spec"> <div class="spec_title"><span>Цвет провода</span></div> <div class="spec_value"><span>Прозрачный</span></div> </div>, <div class="spec"> <div class="spec_title"><span>Максимальный отрезок</span></div> <div class="spec_value"><span>25 м</span></div> </div>, <div class="spec"> <div class="spec_title"><span>Длина бухты</span></div> <div class="spec_value"><span>100 м</span></div> </div>, <div class="spec" style="margin-top:10px;"> <div class="spec_title"><span>Цвета</span></div> <div class="spec_value colors"> <div class="colors_wrapper"> <div class="color_el" title="Синий"><span style="background: #005b94;"></span></div> <div class="color_el" title="Красный"><span style="background: #b91016;"></span></div> <div class="color_el" title="Желтый"><span style="background: #f4d600;"></span></div> <div class="color_el" title="Зеленый"><span style="background: #7ebf00;"></span></div> <div class="color_el" title="Белый"><span style="background: #ffffff;"></span></div> <div class="color_el" title="Теплый белый"><span style="background: #f3f1d6;"></span></div> <div class="color_el" title="RGB"><span style="background: url('/img/product/rgb.png');"></span></div> </div> </div> </div>] 

Generating a dictionary:

  specials = { i.find('div', 'spec_title').text : i.find('div', 'spec_value').text for i in special } 

I get this result:

 {'Тип светодиода': 'DIP 5 мм', 'Шаг': '15 см', 'Кол-во мерцающих светодиодов': '100 шт', 'Цвета': '\n\n\n\n\n\n\n\n\n\n', 'Артикул': 'LED-LP-100M-15CM-12V-F', 'Цвет провода': 'Прозрачный', 'Кол-во светодиодов': '666 шт', 'Рабочее напряжение': '12 вольт', 'Максимальный отрезок': '25 м', 'Длина бухты': '100 м'} 

“Colors” are not on every page, and I would like to check the key before writing to the dictionary in order to start another cycle that would collect all the # colors.

Tell me, is it even possible to perform a key or value check when generating a dictionary?

  • The question is not entirely clear. Try a minimal example to create. If the question is about the dictionary, then remove the html and just make up the list for example. If the question is about html, then reformulate the question and indicate what and from what (minimum) html you want to receive (without specifying how the total data is obtained, but what you want to receive). In any case, indicate an obvious problem with the code: what do you want to get (literal result), what does your code do instead (in detail). - jfs
  • @jfs, the task is to check the key before writing to the dictionary, and if key == something, then start another cycle. - Narnik Gamarnik
  • one
    the fact that you repeat what you have already written, the question will not become clearer. Read my comment again and try to follow its recommendations as much as you can. - jfs

1 answer 1

 obj1 = [[1, 2], [3, 4], [5, 6]] obj2 = [['a', 'b'], ['c', 'd']] dt = {k: v if not k == 3 else {a: b for a, b in obj2} for k, v in obj1} print(dt) >>> {1: 2, 3: {'a': 'b', 'c': 'd'}, 5: 6}