【问题标题】:Extract MEDIA tags from HTML content [closed]从 HTML 内容中提取 MEDIA 标签 [关闭]
【发布时间】:2012-08-13 00:22:28
【问题描述】:

我正在使用CURL 从某些网页中获取内容。我需要从内容中提取媒体标签。

有没有可用的库?或者任何关于制作它的想法都会非常棒。

【问题讨论】:

  • 你有没有努力自己解决这个问题?如果您甚至无法尝试使用谷歌搜索,那么您也不应该在此站点停留。

标签: php html curl


【解决方案1】:

这会有帮助吗?

function file_get_contents_curl($url)
{
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

$html = file_get_contents_curl("http://example.com/");

//parsing begins here:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');

//get and display what you need:
$title = $nodes->item(0)->nodeValue;

$metas = $doc->getElementsByTagName('meta');

for ($i = 0; $i < $metas->length; $i++)
{
    $meta = $metas->item($i);
    if($meta->getAttribute('name') == 'description')
        $description = $meta->getAttribute('content');
    if($meta->getAttribute('name') == 'keywords')
        $keywords = $meta->getAttribute('content');
}

echo "Title: $title". '<br/><br/>';
echo "Description: $description". '<br/><br/>';
echo "Keywords: $keywords";

或者如果您需要保存图像..

$remote_img = 'http://www.example.com/images/image.jpg ';
$img = imagecreatefromjpeg($remote_img);
$path = 'images/';
imagejpeg($img, $path);

function save_image($img,$fullpath){
    $ch = curl_init ($img);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $rawdata=curl_exec($ch);
    curl_close ($ch);
    if(file_exists($fullpath)){
        unlink($fullpath);
    }
    $fp = fopen($fullpath,'x');
    fwrite($fp, $rawdata);
    fclose($fp); 
}

【讨论】:

  • 您可以将 TagName 更改为您想要的任何内容以提取数据
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-08
  • 2019-09-26
  • 1970-01-01
  • 2012-09-03
  • 1970-01-01
相关资源
最近更新 更多