Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Parsowanie - wyciąganie ze źródła tekstu
newb1e
post 22.07.2010, 20:28:15
Post #1





Grupa: Zarejestrowani
Postów: 13
Pomógł: 0
Dołączył: 23.05.2010

Ostrzeżenie: (0%)
-----


Przykładowa część ze źródła pewnej strony:
  1. <div class="left">
  2.  
  3. <h1 class="title">
  4. Seagate 500 GB Barracuda 7200.12 (16MB, Serial ATA II) </h1>
  5.  
  6. <div class="code">
Chciałbym teraz za pomocą preg_match wyciągnąć tekst pomiędzy <h1 class="title"></h1> jednak w żaden sposób nie umiem tego zrobić. Nie chcę tego robić za pomocą strpos.
  1. $zmienna = preg_match(/<h1 class=\"title\">(.*)<\/h1>/i, $zrodlo, $tablica);
Niestety takie coś nie działa. Podejrzewam, że to przez te spacje. W jaki sposób to wyciągnąć? Za pomocą preg_match? Chyba, że jeszcze inny sposób poza strpos.
Go to the top of the page
+Quote Post
athei
post 22.07.2010, 20:37:21
Post #2





Grupa: Zarejestrowani
Postów: 389
Pomógł: 141
Dołączył: 11.04.2009

Ostrzeżenie: (0%)
-----


Modyfikatora "s" zapomniałeś
Kod
/<h1 class=\"title\">(.*)<\/h1>/is

albo
Kod
#<h1 class=\"title\">(.*)</h1>#is

Obrób sobie to co pobierze, bo będą tam znaki nowej linii i spacje.

Ten post edytował athei 22.07.2010, 20:38:27
Go to the top of the page
+Quote Post
newb1e
post 22.07.2010, 20:48:56
Post #3





Grupa: Zarejestrowani
Postów: 13
Pomógł: 0
Dołączył: 23.05.2010

Ostrzeżenie: (0%)
-----


Działa, ale teraz znowu jak pobiorę źródło strony:
  1. $strona = file_get_contents("http://www.strona.pl/test.html");
  2. preg_match('/<h1 class=\"title\">(.*)<\/h1>/is', $strona, $nazwa);
  3. print_r($nazwa);
to tablica jest pusta.
Patrząc w debugerze $strona ma taki wartość po użyciu file_get_contents:
  1. <div class=\"left\">\n\n <h1 class=\"title\">\n Seagate 500 GB Barracuda 7200.12 (16MB, Serial ATA II) </h1>\n\n
Jak w takim razie z tego wyciągnąć Seagate 500 GB Barracuda 7200.12 (16MB, Serial ATA II)?

Ten post edytował newb1e 23.07.2010, 00:23:38
Go to the top of the page
+Quote Post
muk4
post 23.07.2010, 03:16:27
Post #4





Grupa: Zarejestrowani
Postów: 309
Pomógł: 56
Dołączył: 3.11.2006
Skąd: Gliwice

Ostrzeżenie: (0%)
-----


  1. $text = 'XXX'; //Tutaj twoj pobrany HTML
  2. $pattern = '/h1 class="title">(.*)<\/h1>/';
  3. $text = str_replace(PHP_EOL, '', $text);
  4. preg_match($pattern,$text,$matches);
  5. echo $matches[1];


Ten post edytował muk4 23.07.2010, 03:18:33
Go to the top of the page
+Quote Post
newb1e
post 23.07.2010, 18:54:29
Post #5





Grupa: Zarejestrowani
Postów: 13
Pomógł: 0
Dołączył: 23.05.2010

Ostrzeżenie: (0%)
-----


Niestety to też nie działa. Może to problem w moich ustawieniach albo coś. Najlepiej jakbym podał linka i ktoś u siebie by spróbował tylko tu nie mogę podać linka, bo ostatnio dałem i został usunięty biggrin.gif może na PW wysłać komuś?
Go to the top of the page
+Quote Post
muk4
post 26.07.2010, 00:20:51
Post #6





Grupa: Zarejestrowani
Postów: 309
Pomógł: 56
Dołączył: 3.11.2006
Skąd: Gliwice

Ostrzeżenie: (0%)
-----


Poprawiony kod:

  1. $url = 'http://www.komputronik.pl/index.php/product/101512/Peryferia/Monitory_LCD/22_iiyama_ProLite_B2209HDSD_czarny.html';
  2. $text = file_get_contents($url);
  3. $search = array("\n", "\t", " ", " ");
  4. $text = str_replace($search, '', $text);
  5. $text = substr($text, 11680,8500);
  6. $pattern = '/h1 class="title">(.*)<\/h1>/';
  7. preg_match($pattern,$text,$matches);
  8. echo $matches[1];
Go to the top of the page
+Quote Post
wookieb
post 26.07.2010, 09:05:14
Post #7





Grupa: Moderatorzy
Postów: 8 989
Pomógł: 1550
Dołączył: 8.08.2008
Skąd: Słupsk/Gdańsk




PHPQUERY !


--------------------
Go to the top of the page
+Quote Post
newb1e
post 27.07.2010, 00:30:51
Post #8





Grupa: Zarejestrowani
Postów: 13
Pomógł: 0
Dołączył: 23.05.2010

Ostrzeżenie: (0%)
-----


Więc sprawa wygląda następująco:
  1. $url = 'http://www.komputronik.pl/index.php/product/65058/Podzespo_y_PC/Dyski_twarde/Seagate_500_GB_Barracuda_7200_12_16MB_Serial_ATA_II_.html';
  2. $text = file_get_contents($url);
  3. $search = array("\n", "\t");
  4. $text = str_replace($search, '', $text);
  5. $text = substr($text, 20000, 8000);
  6. $pattern = '#h1 class="title">(.*)<\/h1>#is';
  7. preg_match($pattern,$text,$matches);
  8. echo $matches[1];
To działa bez zarzutów.
Natomiast:
  1. $url = 'http://www.komputronik.pl/index.php/product/65058/Podzespo_y_PC/Dyski_twarde/Seagate_500_GB_Barracuda_7200_12_16MB_Serial_ATA_II_.html';
  2. $text = file_get_contents($url);
  3. $search = array("\n", "\t");
  4. $text = str_replace($search, '', $text);
  5. //$text = substr($text, 20000, 8000);
  6. $pattern = '#h1 class="title">(.*)<\/h1>#is';
  7. preg_match($pattern,$text,$matches);
  8. echo $matches[1];
Już nie działa. Mimo, że tekst w $text jest taki sam tylko dłuższy to tablica jest już pusta. Po prostu nie ma wtedy wyników żadnych. Co może być powodem? preg_match nie sięga tak daleko w głąb kodu czy co laugh.gif
Go to the top of the page
+Quote Post
everth
post 27.07.2010, 00:37:45
Post #9





Grupa: Zarejestrowani
Postów: 782
Pomógł: 153
Dołączył: 21.07.2010

Ostrzeżenie: (0%)
-----


Jezu, oblukajcie i zmodyfikujcie sobie ten skrypt


--------------------
Już mi się ani wiedzieć, ani tym bardziej myśleć nie chce.
[Think different]!
Go to the top of the page
+Quote Post
newb1e
post 27.07.2010, 22:37:40
Post #10





Grupa: Zarejestrowani
Postów: 13
Pomógł: 0
Dołączył: 23.05.2010

Ostrzeżenie: (0%)
-----


Ok. Najprostsze rozwiązanie ->
  1. function parseHtml($path)
  2. {
  3. $path = file_get_contents($path);
  4. $doc = new DOMDocument();
  5. @$doc->loadHTML($path);
  6. $xpath = new DOMXPath($doc);
  7. $entries = $xpath->query("//h1[@class='title']");
  8. $resultString;
  9. foreach ($entries as $node) {
  10. $resultString .= $doc->saveXML($node);
  11. }
  12. return $resultString;
  13. }
  14. $path='http://www.komputronik.pl/index.php/product/65058/Podzespo_y_PC/Dyski_twarde/Seagate_500_GB_Barracuda_7200_12_16MB_Serial_ATA_II_.html';
  15. echo parseHtml($path);
Działa idealnie biggrin.gif

Ten post edytował newb1e 27.07.2010, 22:43:19
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 24.06.2025 - 02:37