【问题标题】:How to scrape html+css如何抓取html+css
【发布时间】:2015-06-11 13:42:41
【问题描述】:

什么是用于抓取 HTML 及其 CSS 样式的纯 PHP 解决方案?

比如抓取这个页面

<!doctype html>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<div id="success" class="alert alert-success" role="alert" style="font-size:30px">Success</div>

其中 id=success 将返回

<div style="color: rgb(60, 118, 61); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 30px; line-height: 42.8571434020996px; background-color: rgb(223, 240, 216);">Success</div>

就好像我手动复制了页面的那部分一样。

【问题讨论】:

  • 你可以得到 html 标记,但我不知道 CSS,得到 CSS 规则是另一回事。我要做的是,先获取标记,获取 ID,然后分别解析 CSS
  • 浏览器需要将 css 和 html 解析为一个实体。听起来您正在寻找headless browser

标签: php html css


【解决方案1】:

如果我没有正确理解,您想使用 PHP 将内联样式集添加到 id“成功”。

你可以做一些事情:

<?php

    $inlineStyle = "color: rgb(60, 118, 61); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 30px; line-height: 42.8571434020996px; background-color: rgb(223, 240, 216);";

?>

并将其添加到您的 HTML 标记中:

<div id="success" .... style="<?= $inlineStyle ?>"></div>

【讨论】:

  • 我认为您没有正确理解 - OP 正在尝试 scrape 外部站点。他们无权访问源代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-16
  • 1970-01-01
相关资源
最近更新 更多