donh61500 2016-06-18 19:28
浏览 113
已采纳

从href标签中解析元素

I need some help with my code. I want to parse each element from the streams tags but I cant find out how I could do this.

When I try this:

$streams_url = $xpath->query("//span[@id='streams'"]);

I will get something like this:

serverip page isn’t working

serverip is currently unable to handle this request.

HTTP ERROR 500

Here is the php:

<?php
ini_set('max_execution_time', 300);
$errmsg_arr = array();
$errflag = false;

function getState($string)
{
  $ex = explode(" ",$string);
  return $ex[1];
}

$baseUrl = file_get_contents('http://myserverip/get-listing.php');

$domdoc = new DOMDocument();
$domdoc->strictErrorChecking = false;
$domdoc->recover=true;
@$domdoc->loadHTML($baseUrl);
$links = $domdoc->getElementsByTagName('a');
$i = 0;
$count = 0;
$streams_url = $xpath->query("//span[@id='streams'"]);
echo $streams_url;

$data = array();
>?

Here is the html data:

<a id="link1" href="http://myserverip/getlisting.php?channel=skyatlantic">http://myserverip/getlisting.php?channel=Sky Atlantic&id=108</a><br><br><a id="streams" href="rtmp://www.testserver.com/skyatlantic">Stream 1</a>

Here is what I want to achieve:

http://www.testserver.com/stream01

I want to parse each element from the streams tags.

Can you please show me how i could do this in PHP??

  • 写回答

1条回答 默认 最新

  • donpvtzuux37724 2016-06-18 19:43
    关注

    Since you have the id you are looking for, you don't actually need to use XPath. This will do the job:

    $el = $domdoc->getElementById('streams');
    $url = $el->getAttribute('href');
    

    In comments you mention you have duplicate id values: this is invalid HTML. But you could process them as follows:

    $streams_url = $xpath->query("//*[@id='streams']");
    foreach($streams_url as $a) {
        $url[] = $a->getAttribute("href");
    }
    print_r($url); // array of href values
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100