PHP:获取HTML网页的所有CSS文件

Dor*_*hen 1 html css php

我正在尝试从URL 获取html文件的所有CSS文件.

我知道如果我想获取HTML代码很简单 - 只需使用PHP函数 - file_get_contents.

问题是 - 如果我可以轻松搜索HTML的URL 并从那里获取所有相关CSS文件的文件或内容?

注意 - 我想构建一个用于获取大量CSS文件的引擎,这就是为什么只读源是不够的..

谢谢,

Tom*_*Tom 5

您可以尝试使用http://simplehtmldom.sourceforge.net/进行HTML解析.

require_once 'SimpleHtmlDom/simple_html_dom.php';

$url = 'www.website-to-scan.com';
$website = file_get_html($url);

// You might need to tweak the selector based on the website you are scanning
// Example: some websites don't set the rel attribute
// others might use less instead of css
//
// Some other options:
// link[href] - Any link with a href attribute (might get favicons and other resources but should catch all the css files)
// link[href="*.css*"] - Might miss files that aren't .css extension but return valid css (e.g.: .less, .php, etc)
// link[type="text/css"] - Might miss stylesheets without this attribute set
foreach ($website->find('link[rel="stylesheet"]') as $stylesheet)
{
    $stylesheet_url = $stylesheet->href;

    // Do something with the URL
}
Run Code Online (Sandbox Code Playgroud)