简体   繁体   English

解析 HTML 表格如何使用 PHP 解析单元格和行

[英]Parse HTML Table how cell and rows with PHP

<?php

    include ("simpel_html_dom.php");
    $dom = new \DOMDocument();
    $dom->loadHTML('http://www.arbworld.net/en/moneyway');

    $xpath = new \DomXPath($dom);

    // query the first column with class "value" of the table with class "attributes"
    $elements = $xpath->query('(/table[@class="grid"]//tr[@class="belowHeader"])');

    // iterate through all found td elements
    foreach ($elements as $element) {
        echo $element->nodeValue;
    }
?>

I would to scrape the HTML table but when I run the code the result will be an empty page.我想抓取 HTML 表,但是当我运行代码时,结果将是一个空页面。 How can I resolve it?我该如何解决?

That HTML is badly formed ( according to libxml ) so you need to account for that when loading the source data which, incidentally, you do either with loadHTMLFile to load a url or loadHTML to load a string of HTML.该 HTML 格式错误(根据libxml ),因此您需要在加载源数据时考虑到这一点,顺便说一句,您可以使用loadHTMLFile加载 url 或使用loadHTML加载 HTML 字符串。

$url='http://www.arbworld.net/en/moneyway';
libxml_use_internal_errors( true );
$dom=new DOMDocument;
$dom->validateOnParse=false;
$dom->recover=true;
$dom->strictErrorChecking=false;
$dom->loadHTMLFile( $url );
libxml_clear_errors();


$xp=new DOMXPath( $dom );
$col=$xp->query('//table[@class="grid"]/tr[@class="belowHeader"]/td');

if( $col->length > 0 ){
    foreach( $col as $node )echo $node->textContent;
}

I would like to see it like in IMG and not in this way :我想在 IMG 中看到它,而不是这样:

Romanian Liga I22.Dec 18:00:00 FCSBUniversitat2.063.33.999.9 %€ 2070.1 %€ 00 %€ 0€ 207 22.Dec 18:00:00 Italian Serie A22.Dec 11:30:00 AtalantaAC Milan1.8844.499.7 %€ 21 5580.1 %€ 170.2 %€ 46€ 21 622 22.Dec 11:30:00 English League 221.Dec 15:00:00罗马尼亚联赛 I22.Dec 18:00:00 FCSBUniversitat2.063.33.999.9 %€ 2070.1 %€ 00 %€ 0€ 207 22.Dec 18:00:00 意大利甲级联赛 A22.Dec 11.8994AC 米兰 2070.1 %€ 00 %€ 0€ %€ 21 5580.1 %€ 170.2 %€ 46€ 21 622 22.Dec 11:30:00 英格兰联赛 221.Dec 15:00:00

I would see it in columns and rows我会在列和行中看到它

在此处输入图片说明

Understand?理解?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM