PHP简单Dom HTML -解析一个href列表的麻烦


PHP Simple Dom HTML - Trouble parsing list of a hrefs

我正试图从这个网页中抓取所有id以'system'开头的a/ref: http://www.myfxbook.com/systems

这是我的代码,我只是不能开始工作。我已经摆弄了几个小时了,看了无数的问题。

    include_once( 'simple_html_dom.php' );  
    $url2process = 'http://www.myfxbook.com/systems';
    $html = file_get_html( $url2process );
    $cnt = 0;
    $parent_mark = $html->find('a[id^=system]');
    $cntr = 0;
    foreach( $parent_mark as $element) {
        if( $cntr > 3 ) continue;
        $cntr++;
        $single_html = file_get_html( $element->href );

UPDATE1: Ok,这是一种工作现在,但它似乎只使用最后一个href与正确的id的页面。我需要用这个ID来处理所有这些文件,我在这里遗漏了什么?

您可以像这样使用domdocument ..

$html = file_get_contents('http://www.myfxbook.com/systems');
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html);
libxml_use_internal_errors(false);
$links = $doc->getElementsByTagName('a');
$cnt = 0;
$cntr = 0;
foreach ($links as $link) {
    if(preg_match('~^system~', $link->getAttribute('id'))) {
        if( $cntr > 3 ) {
            continue;
        }
        $cntr++;
        $single_html = file_get_contents($link->getAttribute('href'));
        if (empty($single_html)) {
            echo 'EMPTY'; 
        }
    }
}