Javascript PHP:如何在html(从url)中查找和提取具有src属性的元素
我目前正在使用PHP的curl请求从URL获取内容。在获得检查给定HTML块所需的内容后,找到具有给定样式属性的“视频”,并提取其源src值文本。目前我获得了该页面,但如何获得该值?以下是获取页面的代码:Javascript PHP:如何在html(从url)中查找和提取具有src属性的元素,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我目前正在使用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, CURL
<?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;
使用document.querySelector()
指向元素。然后使用document.getAttribute()
获取src
属性
var video=document.querySelector('.webstarvideo-video-source');
log(video.getAttribute('src')代码>
如果要将视频的SRC作为PHP变量,需要通过检查“type”的位置从字符串中提取:
$output=
';
$type_position=strpos($output,“type=”);
$video_src=substr($output,110,$type_position-112);
echo$video_src;//我需要这个
上例中的110
是SRC属性中不超过并包括左双引号的字符数,112
是另外两个字符,用于补偿右双引号和类型之前的空格
希望这有帮助!:) 在PHP级别:
您可以将正则表达式与PHP类一起使用,也可以使用PHP类:
多姆
用正则表达式
$array = array();
preg_match("/source src=\"([^\"]*)\" type=\"video\/mp4\">/i", $output, $array);
echo $array[1];
有了PHP,您可以使用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
是完整的文本,您可以看到regex正在使用
preg_match_all("/(?<=\<source).*?src=\"([^\"]+)\"/", $output, $all);
print_r($all[1]); // all the links will be in this array
preg\u match\u all(“/(?使用attr选择器,如$(“[scr='I NEED THIS'])”)
您希望在PHP级别还是在javascript(假定这是AJAX调用或类似的东西)中使用它“?@guradio我不知道src@FabienTheSolutionphp或javascript都不重要。ThanksHow我可以把它放在php中吗?在
标记中。我把代码放在script标记中,但是我如何得到结果呢?问题是我不知道视频的来源。我需要的代码是提取100多个视频的源视频,所以这是不适用于该细节。感谢使用DOM输出了大量错误(超过100个),使用正则表达式输出:警告:preg_match():第19行C:\xampp\htdocs\00\get-video2.php中的未知修饰符“p”注意:第20行C:\xampp\htdocs\00\get-video2.php中的未定义偏移量:1我编辑了正则表达式部分的答案。我忘记了为DOM部分转义字符,而没有查看源代码本身,这很难说。该页面是为pornhub@IvanMiranda哈哈,我们是不是很快乐通过编程学习现在下载色情?这不起作用,因为我无法使用file\u get\u html获取页面。我需要使用curl。感谢您编写自己的代码,这只是一个示例,因为没有人知道您的目的。
$array = array();
preg_match("/source src=\"([^\"]*)\" type=\"video\/mp4\">/i", $output, $array);
echo $array[1];
$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);
preg_match_all("/(?<=\<source).*?src=\"([^\"]+)\"/", $output, $all);
print_r($all[1]); // all the links will be in this array