【问题标题】:Simple HTML Dom getting tags values获取标签值的简单 HTML Dom
【发布时间】:2020-10-27 10:27:43
【问题描述】:

我有这样的html代码:

<td class="table-main__odds" data-oid="4ci66xv464x0xbdci3" data-odd="2.18"></td>
<td class="table-main__odds colored" data-oid="4ci66xv498x0x0">
    <span>
        <span>
            <span data-odd="3.68"></span>
        </span>
    </span>
</td>
<td class="table-main__odds" data-oid="4ci66xv464x0xbdci4" data-odd="3.09"></td>
<td class="table-main__odds" data-oid="4ci60xv464x0xbdchn" data-odd="10.35"></td>
<td class="table-main__odds" data-oid="4ci60xv498x0x0" data-odd="6.12"></td>
<td class="table-main__odds colored" data-oid="4ci60xv464x0xbdcho">
    <span>
        <span>
            <span data-odd="1.26"></span>
        </span>
    </span>
</td>

我需要获取数据奇数值,但你可以看到一些值在 td 标签中,一些值在 span 标签中,但都是数据奇数

我正在尝试这种方法:

<?php

include_once('../simple_html_dom.php');

$url = "xyz";
 
function curl_request($url, $timeout = 30) {
    // Initialize curl with given url
    $ch = curl_init($url);

    // Set user-agent
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);

    // Write the response to a variable
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Follow redirects
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    // Max seconds to execute
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

    // Stop on error
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

    return curl_exec($ch);
}

function get_html($url) {
    return str_get_html(curl_request($url));
}

$html = get_html($url);

$b = 0; 

$search = $html->find('td[class=table-main__odds], td[class=table-main__odds colored] span');

foreach($search as $allOdds){
    $quote = array($allOdds->href, $allOdds->innertext);
    if (isset($allOdds->attr['data-odd'])) {
        $quote['data-odd'] = $allOdds->attr['data-odd'];
    }
 
    $quotes[] = $quote;
}


    foreach($quotes as $mark) { 
    echo $mark[0]. " ";  
} 
?>

但我收到以下错误: 致命错误:在第 33 行(foreach 行)中调用非对象中的成员函数 find()

有什么建议吗?

谢谢

编辑:我把 $html = get_html($url);

EDIT2:我添加了 var_dump($quotes);在foreach循环之后

EDIT3: 我的输出是这样的: see new image

【问题讨论】:

  • 变量$html此时不存在。您忽略了调用 get_html 函数并将返回值分配给该变量。
  • 如何更改我的代码?你能帮我举个例子吗?谢谢
  • $html = get_html($url); ...?
  • 好的,我编辑了我的代码,但现在我得到了 array;array;array ...对不起,我是 php 新手,我现在应该做什么?谢谢
  • 在循环之后执行var_dump($quotes); 开始,看看这是否收集了您预期的数据。如果您随后无法以特定的所需格式创建任何有意义的输出 - 那么您应该从阅读一些初学者教程中的如何处理数组开始。 (这不是一个学习基础知识的地方。)

标签: php html web-scraping simple-html-dom


【解决方案1】:

如果您只是想在 html 中获取 data-odd 属性的属性值,请尝试以下简单操作:

$doc = new DOMDocument();
$doc->loadHTML($html);
$xp = new DOMXPath($doc);
$odds = $xp->query('//*[@data-odd]/@data-odd');
foreach ($odds as $odd) {  
    echo($odd->value) . "\r\n";    
}

输出:

2.18

3.68

3.09

10.35

6.12

1.26

【讨论】:

  • 感谢您的回答,请问可以用 url 举例吗?我希望每行有 3 个值。再次感谢
猜你喜欢
  • 1970-01-01
  • 2013-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多