2010-12-31 9 views
2

Hallo, ich bin ein Screen Scrape auf einer Wetter Website zu tun, die in Inline-Styles hat es div und hat keine Klasse oder id hier ist ihr Code:Styling Inline-Stil in php html dom Parser Probleme

<div class="TodaysForecastContainer"> 

        <div class="TodaysForecastContainerInner"> 
         <div style="font-size:12px;"><u>This morning</u></div> 
         <div style="position:absolute;top:17px;left:3px;"> 
          <a href="forecastPublicExtended.asp#Period0" target="_blank"> 
           <img src="./images/wimages/b_cloudy.gif" height="50px" width="50px" alt="weather image">   
          </a>     </div> 
         <div style="position:absolute; top:25px; left:57px; text-align:left; height:47px; width:90px;"> 
          Sunny Breaks       </div> 
        </div> 

        <div class="TodaysForecastContainerInner"> 
         <div style="font-size:12px;"><u>This afternoon</u></div> 
         <div style="position:absolute;top:17px;left:3px;"> 
          <a href="forecastPublicExtended.asp#Period0" target="_blank"> 
           <img src="./images/wimages/b_pcloudy.gif" height="50px" width="50px" alt="weather image">  
          </a>     </div> 
         <div style="position:absolute; top:25px; left:57px; text-align:left; height:47px; width:90px;"> 
          Mix of Sun and Cloud       </div> 
        </div> 

Das Problem ist die absolute Position Inline-Stil und sie haben keine Klasse oder ID, ich hatte gehofft, ich könnte hinzufügen, einen Klassennamen und entfernen Inline-Stil auf Div mit "This morning", div mit dem Bild und entfernen Sie auch den Link und das div mit Beschreibung (ex. Sunny Breaks) ändert auch alle TodaysForecastContainerInner, da es ungefähr 4 Vorhersage hat. so dass es ähnlich wie:

<div class="day>This morning</div><div class="thumbnail"><img src="sample.jpg"></div><div class="description">Sunny Breaks</div> 

Ich war mit:

foreach($html->find('.TodaysForecastContainerInner div') as $e) 
echo $e->innertext . '<br>'; 

, die mit u und img-Tag, I I img nur Stil kann nicht das div mit discription verwenden leben uns alle divs entfernt und Um die anderen beiden Divs zu stylen, bin ich nur ein Anfänger bei PHP. Ich hoffe, jemand könnte mir Ratschläge geben, vielen Dank.

Antwort

1

Überprüfen Sie die phpQuery Bibliothek. Es kann jQuery-ähnliche Manipulation mit PHP durchführen. Dieser Code erfüllt im Wesentlichen, was Sie zu tun versuchen:

<?php 

include 'phpQuery-onefile.php'; 

$text = <<<EOF 
<div class="TodaysForecastContainer"> 
    <div class="TodaysForecastContainerInner"> 
     <div style="font-size:12px;"><u>This morning</u></div> 
     <div style="position:absolute;top:17px;left:3px;"> 
       <a href="forecastPublicExtended.asp#Period0" target="_blank"> 
         <img src="./images/wimages/b_cloudy.gif" height="50px" width="50px" alt="weather image">   
       </a> 
     </div> 
     <div style="position:absolute; top:25px; left:57px; text-align:left; height:47px; width:90px;"> 
      Sunny Breaks 
     </div> 
    </div> 
    <div class="TodaysForecastContainerInner"> 
     <div style="font-size:12px;"><u>This afternoon</u></div> 
     <div style="position:absolute;top:17px;left:3px;"> 
      <a href="forecastPublicExtended.asp#Period0" target="_blank"> 
       <img src="./images/wimages/b_pcloudy.gif" height="50px" width="50px" alt="weather image">  
      </a> 
     </div> 
     <div style="position:absolute; top:25px; left:57px; text-align:left; height:47px; width:90px;"> 
      Mix of Sun and Cloud 
     </div> 
    </div> 
EOF; 

$doc = phpQuery::newDocumentHTML($text); 

$containers = pq('.TodaysForecastContainerInner', $doc); 
foreach($containers as $container) { 
    $div = pq('div', $container); 

    $div->eq(0)->removeAttr('style')->addClass('day')->html(pq('u', $div->eq(0))->html()); 
    $div->eq(1)->removeAttr('style')->addClass('thumbnail')->html(pq('img', $div->eq(1))->removeAttr('height')->removeAttr('width')->removeAttr('alt')); 
    $div->eq(2)->removeAttr('style')->addClass('description'); 
} 

print $doc; 

Ergebnis:

<div class="TodaysForecastContainer"> 
    <div class="TodaysForecastContainerInner"> 
    <div class="day">This morning</div> 
    <div class="thumbnail"><img src="./images/wimages/b_cloudy.gif"></div> 
    <div class="description"> 
     Sunny Breaks 
    </div> 
    </div> 
    <div class="TodaysForecastContainerInner"> 
    <div class="day">This afternoon</div> 
    <div class="thumbnail"><img src="./images/wimages/b_pcloudy.gif"></div> 
    <div class="description"> 
     Mix of Sun and Cloud 
    </div> 
    </div> 
+0

hi danke für deinen Kommentar Ich bekomme Syntaxfehler auf $ text = <<< EOF übrigens was ist EOF Entschuldigung, so neu zu sein – cooldude

+0

Hrm ... Ich habe es einfach nochmal versucht, indem ich es eingefügt und kopiert habe, und es hat für mich geklappt. Kein Problem, aber der "$ text = <<< EOF ... EOF;" fängt alles zwischen <<< EOF und EOF ein ; und weist es als String in $ text. Das war nur ein kurzes Beispiel. Solange Sie den HTML-Code in einer Variablen als String haben, funktioniert der phpQuery-Code _should_. Das Schlüsselstück ist: $ doc = phpQuery: : newDocumentHTML ($ text); Sie können $ text beliebig setzen. – borkweb

0

Es ist einfacher, es auf dem Client als auf dem Server zu tun.

Diese jQuery + Javascript wird Ihre Inline-Styles löschen und einen Klassennamen zu jeder Anwendung:

$(document).ready(function() { 
    var target = $('.TodaysForecastContainerInner div') 
     for(var x=0;x< target.length;x++) { 
       target.eq(x).attr('style',''); 
       target.eq(x).addClass("A_"+x) 
     } 
}) 

Ergebnis:

<div class="TodaysForecastContainerInner"> 
    <div style="" class="A_0"><u>This morning</u></div> 
    <div style="" class="A_1"> 
     <a target="_blank" href="forecastPublicExtended.asp#Period0"> 
      <img height="50px" width="50px" alt="weather image" src="./images/wimages/b_cloudy.gif">   
     </a>     </div> 
    <div style="" class="A_2"> 
     Sunny Breaks       </div> 
</div> 

Sie können die Verwendung ein Stylesheet, um es den Weg zu machen suchen Sie wollen.

+0

danken Ihnen für Ihre Antwort leider bin ich nicht Ihr hier draußen gestellt bekommen ist mein Test-Site http: // j2sdesign. com/rgw/article/20101222/NEWS01/712229951/0/Beispiel/callback.php. kannst du mir zeigen wie du es machst hier ist mein php // finde alle foreach ($ html-> finde ('. TodaysForecastContainerInner div') als $ e) echo $ e-> internertext. '
'; – cooldude

+0

Ich bekomme auch Fehler auf $ (Dokument) .ready (Funktion() {var Ziel = $ (Ich weiß nicht, was falsch ist – cooldude

+0

Hier ist die Quelle, die Sie den PHP anzeigen kann ich legte cooldude