2015-04-07 41 views
0

什麼是一個純粹的PHP解決方案,用於刮取HTML及其CSS樣式?如何刮html + css

例如,刮本頁面

<!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 =成功將返回

<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> 

我彷彿手動複製網頁的一部分。

+0

你可以得到的HTML標記,我不知道CSS雖然得到CSS規則是另一回事。我會做的是,先獲得標記,獲得ID,然後分別解析CSS – Ghost

+0

它需要一個瀏覽器將css和html解析爲一個實體。這聽起來像你正在尋找[無頭瀏覽器](http://stackoverflow.com/a/13424762/622391)。 –

回答

0

如果我沒有正確使用,您希望使用PHP爲ID「sucess」添加內聯樣式集。

你可以做的東西:

<?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> 
+0

我不認爲你的理解正確--OP正試圖[刮](http://en.wikipedia.org/wiki/Web_scraping)一個外部網站。他們無法訪問源代碼。 –