![](/img/trans.png)
[英]Extract URL link from src attribute of script tag in html through XSLT
[英]PHP: How to find and extract an element with src attribute in html (from url)
我目前正在使用PHP的curl请求从URL获取内容。 获取内容后,我需要检查给定的HTML块,找到具有给定样式属性的“视频”,并提取其源src值文本。 目前,我可以获取页面,但是如何获取此值? 这是我获取页面的代码:
<?php
$Url = 'some site';
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init($Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // add this one, it seems to spawn redirect 301 header
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); // spoof
$output = curl_exec($ch);
curl_close($ch);
echo $output;
上面的代码正在工作并输出页面。 然后,在页面的输出中,我检查了元素,发现了这一点:
<div class="webstarvideo">
<video style="width:100%;height:100%" preload="none" class="">
<source src="I NEED THIS" type="video/mp4"></video>
<div class="webstarvideodoul">
<canvas></canvas>
</div>
</div>
我需要上述代码中视频的src,该怎么办?
在PHP级别:
您可以使用带有preg_match的正则表达式或使用PHP DOMDocument类:
DOM
$doc = new DOMDocument();
$doc->loadHTML($output);
$videoSource = $doc->getElementsByTagName('source');
echo $videoSource->getAttribute('src');
使用REGEX
$array = array();
preg_match("/source src=\"([^\"]*)\" type=\"video\/mp4\">/i", $output, $array);
echo $array[1];
如果要将视频的SRC作为PHP变量获取,则需要通过检查“类型”在哪里从字符串中提取它:
$output = '<div class="webstarvideo">
<video style="width:100%;height:100%" preload="none" class="">
<source src="I NEED THIS" type="video/mp4"></video>
<div class="webstarvideodoul">
<canvas></canvas>
</div>
</div>';
$type_position = strpos($output, "type=");
$video_src = substr($output, 110, $type_position - 112);
echo $video_src; // I NEED THIS
上例中的110
是SRC属性中包含左双引号的字符数,而112
是表示右双引号和type
之前的空格的另外两个字符。
希望这可以帮助! :)
借助PHP,您可以使用简单HTML DOM解析器来执行此操作,并查询类似于jQuery的语法。
$Url = 'some site';
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init($Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // add this one, it seems to spawn redirect 301 header
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); // spoof
$output = curl_exec($ch);
curl_close($ch);
$html = str_get_html($output);
$video = $html->find('video', 0);
$videoSrc = $video->src;
var_dump($videoSrc);
假设$output
是完整的文本,您可以正则表达式使用...
preg_match_all("/(?<=\<source).*?src=\"([^\"]+)\"/", $output, $all);
print_r($all[1]); // all the links will be in this array
使用document.querySelector()
指向您的元素,然后使用document.getAttribute()
获得src
属性。
var video = document.querySelector('.webstarvideo video source'); console.log(video.getAttribute('src'));
<div class="webstarvideo"> <video style="width:100%;height:100%" preload="none" class=""> <source src="I NEED THIS" type="video/mp4"></video> <div class="webstarvideodoul"> <canvas></canvas> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.