tmall天猫获取所有商品信息的采集规则(php json)

发现tmall页面的静态内容采用了cdn分片,页面中变化的内容,都放到了cdn里,所有商品的信息都可以在JSON中获取,也许是方便在更新商品的时候,不用更新商品详情页的静态文件吧。
下面的代码可以读取商品的详细信息,并对信息做了简单处理,方便火车采集器采集调用

[php]
<?php
$url = ‘http://tdecorate.tbcdn.cn/dc/fetchDc.htm?pid=72854835&sellerId=130974249’;
$url = ‘http://tdecorate.tbcdn.cn/dc/fetchDc.htm?pid=640211846&sellerId=737591379’;

$html = @file_get_contents($url);
echo tidy($html);

function tidy($html)
{
//过滤js中的换行符
$html = str_replace(array(‘\r’, ‘\n’, ‘\t’, ‘http://a.tbcdn.cn/kissy/1.0.0/build/imglazyload/spaceball.gif’, ‘data-ks-lazyload’),
array("\r", "\n", "\t", ”, ‘src’),
$html);

//Js unicode解码
$html = unicode_decode($html);

//去掉反斜杠转议符
$html = stripslashes($html);

//过滤特殊样式
$html = str_replace(array(‘ src="http://a.tbcdn.cn/kissy/1.0.0/build/imglazyload/spaceball.gif"’, ‘data-ks-lazyload’),
array(”, ‘src’),
$html);

//匹配出内容
$html = get_desc($html);

return trim($html);
}

function unicode_decode($string)
{
$string = preg_replace_callback(‘#\\\u([0-9a-f]{4})#ism’, create_function(‘$matches’, ‘return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");’), $string);
return $string;
}

function get_desc($html)
{
preg_match(‘@"DcTopRight"[\s\S]*?"html":"([\s\S]*?)\s+\},@is’, $html, $match);
return $match[1];
}
[/php]

发表评论