Bonjour,
Je cherche à extraire ce genre de données :
Code html : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 <section itemtype="http://schema.org/LocalBusiness"> <h2 itemprop="name"><a href="">NOM ENTREPRISE 1</a></h2> <address class="addr" itemprop="address" itemscope="" itemtype="http://schema.org/PostalAddress"> <span itemprop="streetAddress">ADRESSE 1</span> <span itemprop="postalCode">CP 1</span> <span itemprop="addressLocality">VILLE 1</span> </address> <p class="telephone" itemprop="telephone">TEL 1</p> </section> <section itemtype="http://schema.org/LocalBusiness"> <h2 itemprop="name"><a href="">NOM ENTREPRISE 2</a></h2> <address class="addr" itemprop="address" itemscope="" itemtype="http://schema.org/PostalAddress"> <span itemprop="streetAddress">ADRESSE 2</span> <span itemprop="postalCode">CP 2</span> <span itemprop="addressLocality">VILLE 2</span> </address> <p class="telephone" itemprop="telephone">TEL 2</p> </section>
J'ai pour le moment ce code :
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 $dom = new DomDocument; $dom -> loadHTML($url); $coords = []; $xpath = new DomXPath($dom); $current = []; foreach ($xpath->query('//*[@itemtype="http://schema.org/LocalBusiness"]') as $n) { foreach ($xpath->query('//*[@itemprop and not(@itemtype)]', $n) as $prop) { $current[$prop->getAttribute('itemprop')] = $prop->nodeValue; } $coords[] = (object) $current; } echo "<hr />Résultats : <br /><br />"; foreach ($coords as $c) { echo $c->name . ' : ' . $c->streetAddress . $c->postalCode . $c->addressLocality . $c->telephone . '<br />'; }
le problème, c'est qu'il me renvoie 2 fois la même donnée comme si il y avait un écrasement.
Voici :
Merci de votre aideRésultats :
NOM ENTREPRISE 2 : ADRESSE 2CP 2VILLE 2TEL 2
NOM ENTREPRISE 2 : ADRESSE 2CP 2VILLE 2TEL 2
Partager