【问题标题】:Fetch the data when compare the time比较时间时获取数据
【发布时间】:2018-05-04 16:43:40
【问题描述】:

我正在开发一个每天连接到该站点的功能,以便获取我想要的数据。我想在 html 源中找到与我当前时间相匹配的时间,所以之后我想获取数据。

代码如下:

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 的输出:

["<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 以匹配或大于它,以便我可以获取以 6:30 pm 开头的数据一直到最后但我不知道该怎么做。

你能告诉我一个例子,我如何使用 $xpath 查询找到时间,以便我可以使用 html 标签获取我想要使用的数据?

编辑:

例如:

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

6:30 pm

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

【问题讨论】:

  • 我们可以在客户端的javascript中做到这一点吗?

标签: php html tags


【解决方案1】:

不太清楚你在问什么。您可以多次内爆和爆炸以获得您正在寻找的细分市场。

$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];

会给你:

<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=”上爆炸,然后在数组的循环中执行 substr 搜索您想要的时间。我认为这更清洁,更容易理解

【讨论】:

  • 非常感谢您,我想知道如何使用 curl 获取 &lt;/a&gt; 标签,因为您只将 html 源代码放入变量中??
  • 嗨,我不知道 curl 并且不完全确定你在问什么。看来您要在 $html 中恢复整个页面。尽管它似乎以数组的形式返回。您可以将其内爆为字符串,然后在
  • 不,它对我不起作用,因为我必须使用 $html = $doc-&gt;getElementsByTagName('a'); 来获取内容。我想获取&lt;/i&gt;&lt;/h2&gt; 之间的标签文本以获取文本UEFA Europa League Highlights&lt;h3&gt;&lt;span 获取文本 Highlights of all matches in the UEFA Europa League. 相同。我该怎么做?
  • 请参阅我已编辑的问题中的示例。
  • 在客户端使用js来做这个怎么样?
猜你喜欢
  • 1970-01-01
  • 2014-08-02
  • 1970-01-01
  • 2018-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-19
相关资源
最近更新 更多