![](/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.