-4

提供されたキーワードを含むURLをWebサイトから取得するにはどうすればよいですか?

例:このページhttp://www.catererglobal.com/rzwritingajobad.html
にあるキーワード(promote、job)のいずれかを含むすべてのアンカーhrefをキャプチャしたい

期待される結果は次のとおりです。

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

4

1 に答える 1

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は私たちの親友です;)

于 2012-05-30T09:38:51.973 に答える