php - 在 PHP 中使用简单的 html dom 抓取数据奇数属性
问题描述
早上好,我需要从网站获取一些数据,我正在尝试一些解决方案,但目前我没有找到合适的解决方案。这是代码:
<tr><td class="h-text-left"><a href="/soccer/peru/liga-1/binacional-llacuabamba/YasJ57j7/" class="in-match"><span><strong>Binacional</strong></span> - <span>Llacuabamba</span></a></td><td class="h-text-center"><a href="/soccer/peru/liga-1/binacional-llacuabamba/YasJ57j7/">2:1</a></td><td class="table-main__odds colored" data-oid="3o4fmxv464x0x9r5fh"><span><span><span data-odd="2.16"></span></span></span></td><td class="table-main__odds" data-oid="3o4fmxv498x0x0" data-odd="3.31"></td><td class="table-main__odds" data-oid="3o4fmxv464x0x9r5fi" data-odd="3.13"></td><td class="h-text-right h-text-no-wrap">Yesterday</td></tr>
<tr><td class="h-text-left"><a href="/soccer/peru/liga-1/carlos-stein-atletico-grau/EwcmMDIc/" class="in-match"><span>Carlos Stein</span> - <span>Grau</span></a></td><td class="h-text-center"><a href="/soccer/peru/liga-1/carlos-stein-atletico-grau/EwcmMDIc/">1:1</a></td><td class="table-main__odds" data-oid="3o4cvxv464x0x9r5a3" data-odd="2.32"></td><td class="table-main__odds colored" data-oid="3o4cvxv498x0x0"><span><span><span data-odd="2.99"></span></span></span></td><td class="table-main__odds" data-oid="3o4cvxv464x0x9r5a4" data-odd="3.10"></td><td class="h-text-right h-text-no-wrap">Yesterday</td></tr>
你可以看到 td class table-main__odds coloured 和 td class table-main__odds; 他们并不总是处于相同的位置。我尝试了这种方法:
...
function print_odd($odd) {
if (array_key_exists('data-odd', $odd->attr)) {
return $odd->attr['data-odd'];
}
return $odd->children(0)->children(0)->children(0)->children(0)->attr['data-odd'];
}
...
$odd1 = print_odd($odds[$b++]);
$odd2 = print_odd($odds[$b++]);
$odd3 = print_odd($odds[$b++]);
...
这段代码工作了几年,但我认为代码发生了一些变化有什么建议吗?
谢谢
编辑:这是页面地址:链接网站
解决方案
我认为问题在于sinner HTML
的<td>
s 在元素之间发生了变化或变化。所以有时你有 a<td data-odd="...
和其他时候你有<td><span...<span data-odd="...
。在这种情况下,也许您可以使用 some 更新您的函数,regex
并从内部 HTMLpreg_match
捕获部分。data-odd="..."
例如:
/*
I assume $odd parameter is a <td> DOMElement
let's say $odd is a <td> with this structure:
<td class="table-main__odds colored" data-oid="3o4cvxv498x0x0">
<span><span><span data-odd="2.99"></span></span></span>
</td>
*/
function print_odd($odd) {
// if <td> has data-odd attribute -> this will do
if (array_key_exists('data-odd', $odd->attr)) {
return $odd->attr['data-odd'];
}
// else, grab inner HTML of td
// see https://stackoverflow.com/questions/2087103/how-to-get-innerhtml-of-domnode/39193507
// maybe like this
$td_html = $odd->C14N();
$regex = '/data-odd=\"([0-9.]+)\"+?/';
preg_match($regex, $td_html, $matches);
if ($matches) {
return $matches[1]; // "2.99" (string)
}
// if nothing is found
return false;
}
推荐阅读
- laravel - 无法在 laravel 中使用 vue.js 更新数据
- sql - 如何删除具有 2 列作为复合主键的多行
- r - 使用 R 中的单元格值将列名定义为参数
- c++ - Compare std::variant with int using C++20 <=> is not a constant expression
- laravel - 关于 laravel 中的验证文件的验证错误
- azure - Azure devops 构建管道 - 发布工件过程
- toit - 如何在 Toit 中生成随机数?
- angular - 我可以像这样链接可观察对象吗?我实现了一些东西,但不确定这是最好的方法吗?
- data-structures - 需要多少探针才能避免散列冲突?
- sql - 从上一行更新记录