如何从网站获取包含关键字的网址?

时间:2012-05-30 09:10:51

标签: php

如何从网站获取包含提供的关键字的网址?

例如:我想捕获此页面上的所有锚点href http://www.catererglobal.com/rzwritingajobad.html
包含任何关键字(推广,工作)

预期结果包括:

http://www.catererglobal.com/recruiters/rz-promote-your-brand http://www.catererglobal.com/recruiters/rz-job-advertising

1 个答案:

答案 0 :(得分:0)

这是我在php =)

中的表现
<?php
$oldSetting = libxml_use_internal_errors( true );
libxml_clear_errors();

$html = new DOMDocument();
$html->loadHtmlFile( 'http://www.catererglobal.com/rzwritingajobad.html' );
$xpath = new DOMXPath( $html );
$links = $xpath->query( '//a' );

foreach ( $links as $link ) {
    $cur = $link->getAttribute( 'href' );
    if (preg_match('/(promote|job)/', $cur)) { echo "$cur\n"; }
}

libxml_clear_errors();
libxml_use_internal_errors( $oldSetting );
?>

输出结果为:

http://www.catererglobal.com/recruiters/rz-job-advertising/10298792/post-a-job/
/recruiters/rz-job-advertising
/recruiters/rz-promote-your-brand
/moreterms/job-location
http://www.madgex.com/job-boards/

Xpath是我们最好的朋友;)

相关问题