PHP简单DOMDocument抓取排除td类
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP简单DOMDocument抓取排除td类,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2407字,纯文字阅读大概需要4分钟。
内容图文
![PHP简单DOMDocument抓取排除td类](/upload/InfoBanner/zyjiaocheng/686/c471c0d2f9a843788ce19adfda7dc6eb.jpg)
我只是试图获取所有的< td>位于< tr>内部的元素数据元素.我的问题是因为我试图抓取的表结构是我需要排除所有具有COLLSPAN属性的元素,即< td collspan = 12>
从下面的代码可以看出,获取表数据非常简单,但是由于表结构的原因,我需要排除所有collspan属性.
<?php
$html = file_get_contents('http://www.superxv.com/fixtures/'); //get the html returned from the following url
$game_doc = new DOMDocument();
libxml_use_internal_errors(TRUE); //disable libxml errors
if(!empty($html)) { //if any html is actually returned
$game_doc->loadHTML($html);
libxml_clear_errors(); //remove error
$xpath = new DOMXPath($game_doc);
// Modify the XPath query to match the content
foreach ($xpath->query('//table')->item(0)->getElementsByTagName('tr') as $rows) {
$cells = $rows->getElementsByTagName('td');
//$cells2 = $rows->getElementsByTagName('th');
echo '<pre>';
//@ signs are added due to table structure
//Get scrapped columns
echo $dayDateBye[] = $cells->item(0)->textContent;
echo $homeTeam[] = $cells->item(1)->textContent;
echo $awayTeam[] = $cells->item(2)->textContent;
echo $venue[] = $cells->item(3)->textContent;
echo $timeGMT[] = $cells->item(5)->textContent;
echo $timeZA[] = $cells->item(10)->textContent;
echo '</pre>';
}
}
在这里,您可以看到表格结构,其中显示了5个奇数行的灯具,然后在新的一周开始时更改了结构.我可以识别的跳过结构变化的元素都是< td collspan = 12>.元素.这很棘手,因为TD元素没有类名,而仅具有用于标识它的元素.
任何输入表示赞赏.
解决方法:
您可以按标签长度跳过那些
<?php
$html = file_get_contents('http://www.superxv.com/fixtures/'); //get the html returned from the following url
$game_doc = new DOMDocument();
libxml_use_internal_errors(TRUE); //disable libxml errors
if(!empty($html)) { //if any html is actually returned
$game_doc->loadHTML($html);
libxml_clear_errors(); //remove error
$xpath = new DOMXPath($game_doc);
// Modify the XPath query to match the content
foreach ($xpath->query('//table')->item(0)->getElementsByTagName('tr') as $rows) {
$cells = $rows->getElementsByTagName('td');
if( $cells->length > 1 ){
//$cells2 = $rows->getElementsByTagName('th');
echo '<pre>';
//@ signs are added due to table structure
//Get scrapped columns
echo $dayDateBye[] = $cells->item(0)->textContent;
echo $homeTeam[] = $cells->item(1)->textContent;
echo $awayTeam[] = $cells->item(2)->textContent;
echo $venue[] = $cells->item(3)->textContent;
echo $timeGMT[] = $cells->item(5)->textContent;
echo $timeZA[] = $cells->item(10)->textContent;
echo '</pre>';
}
}
}
?>
内容总结
以上是互联网集市为您收集整理的PHP简单DOMDocument抓取排除td类全部内容,希望文章能够帮你解决PHP简单DOMDocument抓取排除td类所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。