Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] Pobieranie przez DOM
shtoc
post
Post #1





Grupa: Zarejestrowani
Postów: 128
Pomógł: 0
Dołączył: 18.11.2008
Skąd: Polska

Ostrzeżenie: (10%)
X----


Mam pytanie chcę pobrać ze strony za pomocą DOM znacznik "href" ale nie wychodzi możne ktoś widzi błąd ?

fragment strony:

  1. <table cellspacing="1" cellpadding="3" border="0" width="100%">
  2. </thead>
  3. <tr class="odd">
  4. <td>000000</td>
  5. <td>
  6.  
  7. <a title="TYTUŁ" href="http://www.domena.pl/">NAZWA</a>
  8. <br/>
  9.  
  10. </td>
  11. <td>MIASTO</td>
  12. </tr>



Kod php:

  1. <?php
  2. $pol=mysql_connect('localhost', 'root', '') or die('BÄąâ₏šĂ„â₏Œd poÄąâ₏šĂ„â₏Œczenia: '.mysql_error());
  3. mysql_selectdb('krs') or die('BÄąâ₏šĂ„â₏Œd wyboru bazy: '.mysql_error());
  4. mysql_query("SET NAMES utf8");
  5.  
  6. $adres="http://www.domena.pl/";
  7.  
  8. $url = curl_init($adres);
  9. curl_setopt($url, CURLOPT_USERAGENT, 'holmes/3.10.1 (OnetSzukaj/5.0; +http://szukaj.onet.pl)');
  10. curl_setopt($url, CURLOPT_RETURNTRANSFER, 1);
  11. curl_setopt($url, CURLOPT_CONNECTTIMEOUT, 5);
  12. curl_setopt($url, CURLOPT_HEADER, 0);
  13. $strona=curl_exec($url);
  14.  
  15.  
  16. $DOM = new DOMDocument();
  17. @$DOM->LoadHTML(iconv("UTF-8","UTF-8",$strona));
  18.  
  19.  
  20.        foreach($DOM->getElementsByTagName('tr') as $tr)
  21.        {            
  22.            $class = $tr->getAttribute('class');
  23.            if($class == 'odd' OR $class == 'add')
  24.            {
  25.                    foreach($td->getElementsByTagName('a') as $a)
  26.                    {
  27.                    $href = $a->getAttribute('href');                
  28.                    echo $href;
  29.                  
  30.                    //$sql="INSERT INTO krs (`adres`) VALUE ('$href')";
  31.                    //$dane = mysql_query($sql);
  32.                    //echo mysql_errno() . ": " . mysql_error();
  33.                    unset($href,$a);
  34.                    }
  35.  
  36.            }
  37.        
  38.        }
  39. ?>


Ten post edytował shtoc 1.06.2009, 14:27:34
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
shtoc
post
Post #2





Grupa: Zarejestrowani
Postów: 128
Pomógł: 0
Dołączył: 18.11.2008
Skąd: Polska

Ostrzeżenie: (10%)
X----


Mam jeszcze jeden problem również z DOM
a mianowicie :
Kod

    

<div id="top_produkt_firma">

  <div style="float:left">

<h1>NAzwa</h1>

    <table cellpadding="3" cellspacing="0">
    <tr><td><strong>NR:</strong></td><td>00000000000</td></tr>

    <tr><td><strong>Adres:</strong></td><td>Polna 5a<br />
40-301 Krakow</td></tr>
    <tr><td><strong>TEl:</strong></td><td>15464654</td></tr>
    </table>    
    </div>


Chciałem pobrać wartości NR: ADRES: TEL:
Niestety nie wychodzi mi czy ktoś widzi gdzieś błąd (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)


za pomocą
  1. <?php
  2. $pol=mysql_connect('localhost', 'root', '') or die('BÄąâ₏šĂ„â₏Œd poÄąâ₏šĂ„â₏Œczenia: '.mysql_error());
  3. mysql_selectdb('krs') or die('BÄąâ₏šĂ„â₏Œd wyboru bazy: '.mysql_error());
  4. mysql_query("SET NAMES utf8\");
  5.  
  6.  
  7. $adres="http://www.doeman.pl/";
  8.  
  9. $url = curl_init($adres);
  10. curl_setopt($url, CURLOPT_USERAGENT,'');
  11. curl_setopt($url, CURLOPT_RETURNTRANSFER, 1);
  12. curl_setopt($url, CURLOPT_CONNECTTIMEOUT, 5);
  13. curl_setopt($url, CURLOPT_HEADER, 0);
  14. $strona=curl_exec($url);
  15.  
  16.  
  17. $DOM = new DOMDocument();
  18. $DOM->LoadHTML(&#092;"$strona\");
  19.  
  20.  
  21. foreach($DOM->getElementsByTagName('div') as $div)
  22.    {
  23.        $id = $div ->getAttribute('id');
  24.        if($id == 'top_produkt_firma')
  25.        {
  26.        foreach($div->getElementsByTagName('div') as $div1)
  27.        {
  28.            $style = $div1->getAttribute('style');
  29.            if ($style == 'float:left')
  30.            {
  31.                 foreach($div1->getElementsByTagName('table') as $table)
  32.                    {
  33.                        foreach($table->getElementsByTagName('tbody') as $tbody)
  34.                        {
  35.                            foreach($tbody->getElementsByTagName('tr') as $tr)
  36.                            {
  37.                               foreach($tr->getElementsByTagName('td') as $td)
  38.                                {
  39.  
  40.                                        $dane = $td->nodeValue;
  41.                                        print_r($dane);
  42.                                        
  43.                                  
  44.                                }
  45.  
  46.                            }
  47.  
  48.                        }
  49.  
  50.                    }            
  51.        
  52.            }
  53.        }
  54.  
  55.        }
  56.    }
  57.  
  58. ?>


Ten post edytował shtoc 2.06.2009, 08:10:13
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 5.10.2025 - 04:33