首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >比较时间时获取数据

比较时间时获取数据
EN

Stack Overflow用户
提问于 2018-05-04 16:43:40
回答 1查看 46关注 0票数 2

我每天都在做一个连接到网站的函数,这样我就可以获取我想要的数据了。我希望在html源文件中找到与当前时间相匹配的时间,因此在此之后要获取数据。

以下是代码:

代码语言:javascript
复制
function get_shows($channel_id, $day, $skip_finished = true) 
{
   global $day;

   $url = 'http://example.com/api/GS?cid=' . $channel_id . '&offset=+00.00&day=' .$day;

   $curl = curl_init();
   curl_setopt_array($curl, array(
        CURLOPT_USERAGENT => '',
        CURLOPT_TIMEOUT => 30,
        CURLOPT_CONNECTTIMEOUT => 30,
        CURLOPT_HEADER => false,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_MAXREDIRS => 5,
        CURLOPT_URL => $url,
    ));

    $html = curl_exec($curl);
    curl_close($curl);

    // create domdocument from html retrieved
    $doc = new DOMDocument();
    $doc->preserveWhiteSpace = false;
    @$doc->loadHTML($html);
    $xpath = new DOMXPath($doc);
    }
    ?>

下面是$html的输出:

代码语言:javascript
复制
["<a style='width: 149px;' data-time='6:00 pm' <i class=\"c-icon c-8\" >
</i> UEFA Europa League Highlights </h2> <h3>Highlights of all matches in the UEFA Europa League.
<span  >7.0</span></h3></a><a style='width: 149px;' data-time='6:30 pm' <h2><i class=\"c-icon c-8\" >
</i> UEFA Europa League Highlights </h2> <h3>Highlights of all matches in the UEFA Europa League.<span  >7.0</span></a>"]

如果我当前的时间是18:30,我想在html源代码中找到匹配它的时间,或者更长的时间,我可以获取启动6:30 pm的数据,直到结束,但是我不知道如何做到这一点。

您能给我举个例子吗?我如何找到使用$xpath查询的时间,这样我就可以在html标记中获取我想要使用的数据了?

编辑:

举个例子:

如果我的时间是下午6:30,那么我想取标签,使其显示如下:

代码语言:javascript
复制
6:30 pm

UEFA Europa League Highlights
Highlights of all matches in the UEFA Europa League.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-04 17:09:32

不太清楚你在问什么。你可以内爆和爆炸多次,以获得你想要的片段。

代码语言:javascript
复制
$html = ["<a style='width: 149px;' data-time='6:00 pm' <i class=\"c-icon c-8\" >
</i> UEFA Europa League Highlights </h2> <h3>Highlights of all matches in the UEFA Europa League.
<span  >7.0</span></h3></a><a style='width: 149px;' data-time='6:30 pm' <h2><i class=\"c-icon c-8\" >
</i> UEFA Europa League Highlights </h2> <h3>Highlights of all matches in the UEFA Europa League.<span  >7.0</span></a>"];

$htotal=implode("<a",$html);
$harray=explode("'6:30 pm'",$htotal);
$hhalf = $harray[count($harray)-1];
$hspecific = explode("</a",$hhalf);

echo $hspecific[0];

会给你:

代码语言:javascript
复制
<h2><i class="c-icon c-8" >
</i> UEFA Europa League Highlights </h2> <h3>Highlights of all matches in 
the UEFA Europa League.<span  >7.0</span>

如果这是你想要的,我会在'data- time =‘上爆炸,然后在数组的循环中搜索你想要的时间。我觉得这更干净,更容易理解

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50179665

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档