我试图将抓取的网站的每个元素分组,将其转换为json元素,但似乎不起作用。
<?php
// Include the php dom parser
include_once 'simple_html_dom.php';
header('Content-type: application/json');
// Create DOM from URL or file
$html = file_get_html('urlhere');
foreach($html->find('hr ul') as $ul)
{
foreach($ul->find('div.product') as $li)
$data[$count]['products'][]['li']= $li->innertext;
$count++;
}
echo json_encode($data);
?>
字符串
这将返回
{"":{"products":[{"li":" <a class=\"th\" href=\"\/products\/56942-haters-crewneck-sweatshirt\"> <div style=\"background-image:url('http:\/\/s0.merchdirect.com\/images\/15814\/v600_B_AltApparel_Crew.png');\"> <img src=\"http:\/\/s0.com\/images\/6398\/product-image-placeholder-600.png\"> <\/div> <\/a> <div class=\"panel panel-info\" style=\"display: none;\"> <div class=\"name\"> <a href=\"\/products\/56942-haters-crewneck-sweatshirt\"> Haters Crewneck Sweatshirt <\/a> <\/div> <div class=\"subtitle\"> $60.00 <\/div> <\/div> "}
型
当我真的希望实现:
{"products":[{
"link":"/products/56942-haters-crewneck-sweatshirt",
"image":"http://s0.com/images/15814/v600_B_AltApparel_Crew.png",
"name":"Haters Crewneck Sweatshirt",
"subtitle":"60.00"}
]}
型
我如何去除所有冗余信息,并在重新格式化的json中命名每个元素?
谢谢你,谢谢
1条答案
按热度按时间m528fe3b1#
你只需要在内部循环中扩展你的逻辑:
字符串