我是php和dom解析器的新手,我想从多个url中获取单元格的内容。
我可以很好地从一个网址中获取表格元素。
这是我的代码:
<?php
include('../simple_html_dom.php');
$str = file_get_html("http://www.link1.com/");
$html = str_get_html($str);
$tds = $html->find('table',1)->find('td');
$num = null;
foreach($tds as $td){
if($td->plaintext == '123 - xyz'){
$next_td = $td->next_sibling();
$next_td1 = $next_td->next_sibling();
$next_td2 = $next_td1->next_sibling();
$next_td3 = $next_td2->next_sibling();
$num = $next_td3->plaintext ;
break;
}
}
echo($num);
?>
网址我给了内容2表,另外3个网址(如http://www.link2.com/)我想给内容2表 我希望它需要在这些代码上使用数组和循环
有人帮我把它击出来
4天后,我发现了一些接近我要求的东西,但最终无法到达.......
<?php
ini_set('max_execution_time', 300);
include('../simple_html_dom.php');
$links = array (
'https://www.link1.com',
'https://www.link2.com',
'https://www.link3.com',
'https://www.link4.com');
$data = array();
foreach ($links as $link){
$str = file_get_html("$link");
$html = str_get_html($str);
$tds = $html->find('table',1)->find('td');
$num = null;
foreach($tds as $td){
if($td->plaintext == '123 - xyz'){
$next_td = $td->next_sibling();
$next_td1 = $next_td->next_sibling();
$next_td2 = $next_td1->next_sibling();
$next_td3 = $next_td2->next_sibling();
$num = $next_td3->plaintext ;
break;
}
}
//echo($num." ");
$data[] = $num;
$str->clear();
}
print_r($data);
?>
输出就像这样
数组([0] =&gt; 10.11 [1] =&gt; 10.07 [2] =&gt; 9.37 [3] =&gt; 11.43)
现在我想添加数据数组的元素。数组元素是时间,我想将这些元素添加为时间
就像
一样
10.11
10.07
9.37
11.43
41:38(我想要这个输出)
有人请帮助我