A html
halaman yang akan dirangkak:
$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'>主營(yíng):</span>
<span class='propValue fk-prop-other' style='text-decoration:none;font-family:微軟雅黑;font-weight:normal;'>大眾 新速騰 新邁騰 新桑塔納 新捷達(dá) CC 新帕薩特 新寶來(lái) 收事故車及殘值(郭京漢)</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;'>城環(huán)城B-01號(hào)</span>
</p>
</td>
</tr>
</table>
</p>
EOT;
B. Regex merangkak yang saya tulis:
<?php
// 去掉開(kāi)頭和結(jié)尾多余的無(wú)用字符
$str2 = trim($str2);
// 去掉換行符
$str2 = preg_replace('/\r\n|\n\r|\r|\n/' , '' , $str2);
// 抓取正則
$reg_list = "/<ps*topClassName='top1's*topSwitch='on's*productId='(d+)'s*productName='[x7f-xff]+'s*class='productPicListForm's*id='[A-z0-9]+'s*faiWidth='d+'s*faiHeight='d+'s*faiWidthOr='d+'s*faiHeightOr='d+'>s*<tables*id='w+'s*class='formTable's*cellpadding='d*'s*cellspacing='d+'>s*<tr>s*<tds*class='imgp'>s*<tables*cellpadding='d+'s*cellspacing='d+'>s*<tr>s*<td>s*<as*hidefocus='true's*href='[A-z0-9. -_]+'s*target='(_blank|_self)'>s*<imgs*alt='([x7f-xff]+)'s*title='[x7f-xffA-z0-9-]*'s*src='((http|https)://[A-z0-9./!-]+)'s*/>s*</a>s*</td>s*</tr>s*</table>s*</td>s*<tds*style='[A-z0-9:-_#]*'s*class='propList'>s*<tds*style='[A-z0-9:-_#]*'s*class='propList'>s*<p style='[A-z0-9:-_#]*' class='propps*productNames*'>s*<as*class='fk-productName's*hidefocus='true's*href='([A-z0-9-._]+)'s*target='(_blank|_self)'s*title='[x7f-xff]+'>[x7f-xff]+</a>s*</p>s*<ps*class='propps*productPropd*s*'>s*<spans*class='propNames*fk-prop-name'>主營(yíng):</span>s*<spans*class='propValues*fk-prop-other's*style='[A-z0-9x7f-xff: -_#;]*'>([x7f-xff ]+_-.)</span>s*</p>s*<ps*class='propps*productProp4s*'>s*<spans*class='propNames*fk-prop-name'>地址:</span>s*<spans*class='propValues*fk-prop-other's*style='[A-z0-9x7f-xff: -_#;]*'>([x7f-xff -_.]+)</span>s*</p>s*</td>s*</td>s*</tr>s*</table>s*</p>/"
;
Dipaparkan dalam editor (tangkapan skrin):
Sebab mengapa ia begitu rumit adalah kerana saya ingin mendapatkan beberapa kandungan:
Inilah perkara utama. Ia sangat rumit sehingga mustahil untuk menyahpepijat regex supaya ia boleh menangkap kandungan yang saya mahukan dengan betul anda mahukan? ?
Sudah tiba masanya untuk menggunakan kaedah symfony/dom-crawler sekali lagi. DOM Dafa bagus, jika anda berundur daripada peraturan, anda akan kekal selamat
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'>主營(yíng):</span>
<span class='propValue fk-prop-other' style='text-decoration:none;font-family:微軟雅黑;font-weight:normal;'>大眾 新速騰 新邁騰 新桑塔納 新捷達(dá) CC 新帕薩特 新寶來(lái) 收事故車及殘值(郭京漢)</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;'>城環(huán)城B-01號(hào)</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>';
Output
DOM sememangnya pilihan yang lebih baik, tetapi tidak semestinya mustahil untuk melakukannya dengan kaedah biasa.
/alt=\'(?P<name>[^\']+)\'(?:.+?)?src=\'(?P<img>[^\']+)\'(?:.+?)?主營(yíng):(?:.+?)?\'>(?P<scope>[^<]+)(?:.+?)?地址:(?:.+?)?\'>(?P<address>[^<]+)/s