对于一个大学项目,我正在创建一个带有一些后端算法的网站,为了在演示环境中测试这些算法,我需要大量的假数据。为了获得这些数据,我打算刮一些站点。其中一个站点是freelance.com.To,我使用简单的HTML提取数据,但到目前为止,我在实际获取所需数据的努力中没有成功。
下面是我打算抓取的页面的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;
}
}
?>希望有人能给我指明正确的方向,让我知道我是如何做到这一点的。
谢谢。
发布于 2013-11-07 22:19:53
原始源代码是不同的,这就是为什么你没有得到预期的结果.
您可以使用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);演示
https://stackoverflow.com/questions/19844054
复制相似问题