def parse_product(self, response): product = Product() product['url'] = response.url # NAME name = xtract(response, "//h1/text()") add_field(product, 'name', name) # ITEM NUM item_num = xtract(response, xcontains('span', 'style-number')) add_field(product, 'item_num', item_num, lambda x: x.split('#')[-1].strip()) # PRICE price = xtract(response, xcontains('span', 'price-current')) if len(price) == 0: price = xtract(response, xcontains('span', 'regular-price')) add_field(product, 'price', price, to_float) data_script = response.xpath("//script[contains(., 'initialData')]/text()").extract() if len(data_script) > 0: product['initial_data'] = transform_initial_data(data_script[0]) yield product
def test_json_transform(self): test_str = "React.render(React.createElement(ProductDesktop, {\"initialData\":{\"Model\":{\"StyleModel\":{\"Id\":4180678}, \"ApiVersion\":null}}}), document.getElementById( 'main' ));" val = transform_initial_data(test_str) assert len(val.keys()) > 0