我每天都在做一个连接到网站的函数,这样我就可以获取我想要的数据了。我希望在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的数据,直到结束,但是我不知道如何做到这一点。
您能给我举个例子吗?我如何找到使用$xpath查询的时间,这样我就可以在html标记中获取我想要使用的数据了?
编辑:
举个例子:
如果我的时间是下午6:30,那么我想取标签,使其显示如下:
6:30 pm
UEFA Europa League Highlights
Highlights of all matches in the UEFA Europa League.发布于 2018-05-04 17:09:32
不太清楚你在问什么。你可以内爆和爆炸多次,以获得你想要的片段。
$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 =‘上爆炸,然后在数组的循环中搜索你想要的时间。我觉得这更干净,更容易理解
https://stackoverflow.com/questions/50179665
复制相似问题