黄舟2017-05-16 13:18:30
又要祭獻symfony/dom-crawler大法了。 DOM大法好,退正則保平安
composer.json
{
"require": {
"symfony/css-selector": "^3.2",
"symfony/dom-crawler": "^3.2"
}
}
a.php
<?php
/**
* Created by PhpStorm.
* User: shellus
* Date: 2017/4/11
* Time: 11:55
*/
use Symfony\Component\DomCrawler\Crawler;
require 'vendor/autoload.php';
$str2 = <<<EOT
<p topClassName='top1' topSwitch='on' productId='2256' productName='⑧北京汉奥汽配有限公司' class='productPicListForm' id='module406product2256' faiWidth='160' faiHeight='101' faiWidthOr='600' faiHeightOr='382'>
<table id='formTable2256_module406' class='formTable' cellpadding='0' cellspacing='0'>
<tr>
<td class='imgp'>
<table cellpadding='0' cellspacing='0'>
<tr>
<td>
<a hidefocus='true' href='h-pd-2256-2_406.html' target='_blank'>
<img alt='⑧北京汉奥汽配有限公司' title='' src='http://5003100.s21i-5.faiusr.com/2/ABUIABACGAAggsfqwAUotY6_qgUw2AQ4-gI!160x160.jpg' />
</a>
</td>
</tr>
</table>
</td>
<td style='' class='propList'>
<p style='' class='propp productName '>
<a class='fk-productName' hidefocus='true' href='h-pd-2256-2_406.html' target='_blank' title='⑧北京汉奥汽配有限公司'>⑧北京汉奥汽配有限公司</a>
</p>
<p class='propp productProp6 '>
<span class='propName fk-prop-name'>主营:</span>
<span class='propValue fk-prop-other' style='text-decoration:none;font-family:微软雅黑;font-weight:normal;'>大众 新速腾 新迈腾 新桑塔纳 新捷达 CC 新帕萨特 新宝来 收事故车及残值(郭京汉)</span>
</p>
<p class='propp productProp4 '>
<span class='propName fk-prop-name'>地址:</span>
<span class='propValue fk-prop-other' style='text-decoration:none;font-family:微软雅黑;font-weight:normal;'>城环城B-01号</span>
</p>
</td>
</tr>
</table>
</p>
EOT;
$dom = new Crawler();
$dom->addHtmlContent($str2, 'UTF-8');
$imgSrc = $dom->filter('td.imgp')->filter('img')->attr('src');
$productName = $dom->filter('td.propList')->filter('p.productName>a')->text();
$productProp6 = $dom->filter('td.propList')->filter('p.productProp6>.propValue')->text();
$productProp4 = $dom->filter('td.propList')->filter('p.productProp4>.propValue')->text();
var_dump($imgSrc);
echo '<br>';
var_dump($productName);
echo '<br>';
var_dump($productProp6);
echo '<br>';
var_dump($productProp4);
echo '<br>';
輸出
回覆
0