gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:03:05 25 4
gpt4 key购买 nike

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

我知道如果我想获得 HTML 代码很容易 - 只需使用 PHP 函数 - file_get_contents

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

Note - I want to build an engine for getting a lot of CSS files, this is why just reading the source is not enough..

谢谢,

最佳答案

您可以尝试使用 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
}

关于PHP:获取 HTML 网页的所有 CSS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18748052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com