1

对于一个大学项目,我正在创建一个包含一些后端算法的网站,并在演示环境中测试这些算法,我需要大量的假数据。为了获得这些数据,我打算抓取一些网站。其中一个站点是 freelance.com。为了提取数据,我使用了简单的 HTML DOM 解析器,但到目前为止,我在实际获取所需数据方面的努力一直没有成功。

这是我打算抓取的页面的 HTML 布局示例。红色框标记所需数据。

Freelance.com 上的 HTML 代码截图

这是我在遵循一些教程后编写的代码。

<?php
include "simple_html_dom.php";
// Create DOM from URL
$html = file_get_html('http://www.freelancer.com/jobs/Website-Design/1/');

//Get all data inside the <tr> of <table id="project_table">
foreach($html->find('table[id=project_table] tr') as $tr) {

    foreach($tr->find('td[class=title-col]') as $t) {
        //get the inner HTML
        $data = $t->outertext;
        echo $data;
    }
}

?>

希望有人可以为我指明正确的方向,以了解如何使这项工作正常进行。

谢谢。

4

1 回答 1

1

原始源代码不同,这就是为什么你没有得到预期的结果......

您可以使用 来检查原始源代码ctrl+u,数据在 中table[id=project_table_static],并且单元格td没有属性,因此,这是一个从表中获取所有 URL 的工作代码:

$url = 'http://www.freelancer.com/jobs/Website-Design/1/';
// Create DOM from URL
$html = file_get_html($url);

//Get all data inside the <tr> of <table id="project_table">
foreach($html->find('table#project_table_static tbody tr') as $i=>$tr) {

    // Skip the first empty element
    if ($i==0) {
        continue;
    }

    echo "<br/>\$i=".$i;

    // get the first anchor
    $anchor = $tr->find('a', 0);
    echo " => ".$anchor->href;
}

// Clear dom object
$html->clear(); 
unset($html);

演示

于 2013-11-07T22:19:53.367 回答