I have been playing around with the PHP simple HTML DOM parser most of the day successfully. I now want to scrape text from a page which does not have clear identifiers down to the lowest level.
I managed to bring it down to:
</pre><pre> </pre><p><strong>De heerlijke Pure Daghappen v</strong><strong>an Bistro Puur</strong></p> <p><strong>Voor de vaste lage prijs van slechts € 8,50 !</strong></p> <p>geserveerd tussen 17:00 uur & 18:30 uur</p> <p> </p> <h2> <strong>De heerlijke Pure Daghappen voor week 32 zijn:</strong></h2> <p><strong><u>Dinsdag 22 Augustus:</u></strong></p> <p><strong>Gegrilde vakenshaas medaillons met champignonsaus</strong></p> <p> <strong><u>Woensdag 23 Augustus:</u></strong> </p> <p><strong>Gekonfijte scharrelhoen bout met truffelsaus</strong></p> <p> <strong><u>Donderdag 24 Augustus: </u></strong> </p> <p> <br /> <strong>Gebakken zalmfilet met witte wijnsaus</strong></p> <p> <strong><u>Vrijdag 25 Augustus;</u></strong></p> <p><strong>In de oven gebakken kabeljauw filet met pesto saus</strong></p> <p> </p> <p>Alle heerlijke Pure Daghappen worden vergezeld door een aardappelgarnituur en een frisse salade.<br /> Daghap is exclusief bijpassende wijn.</p> <p> </p> <p><strong>Wij verwelkomen u graag!</strong></p> <p><em><strong>Team Bistro Puur</strong></em></p> <p class="rteindent1 rtecenter"><strong> </strong></p> an Bistro Puur
by using
$html = file_get_html('http://www.bistropuur.nl/pure-daghappen');
foreach($html->find('div[class=node-content]') as $e){
$temp = [$e->innertext];
}
I subsequently want to create an array that contains all the different pieces of text to allow further manipulation towards something like:
$data = array[Dinsdag 22 Augustus, Gegrilde vakenshaas medaillons met champignonsaus, ....].
I tried the following:
$html = '</pre><pre> </pre><p><strong>De heerlijke Pure Daghappen v</strong><strong>an Bistro Puur</strong></p> <p><strong>Voor de vaste lage prijs van slechts € 8,50 !</strong></p> <p>geserveerd tussen 17:00 uur & 18:30 uur</p> <p> </p> <h2> <strong>De heerlijke Pure Daghappen voor week 32 zijn:</strong></h2> <p><strong><u>Dinsdag 22 Augustus:</u></strong></p> <p><strong>Gegrilde vakenshaas medaillons met champignonsaus</strong></p> <p> <strong><u>Woensdag 23 Augustus:</u></strong> </p> <p><strong>Gekonfijte scharrelhoen bout met truffelsaus</strong></p> <p> <strong><u>Donderdag 24 Augustus: </u></strong> </p> <p> <br /> <strong>Gebakken zalmfilet met witte wijnsaus</strong></p> <p> <strong><u>Vrijdag 25 Augustus;</u></strong></p> <p><strong>In de oven gebakken kabeljauw filet met pesto saus</strong></p> <p> </p> <p>Alle heerlijke Pure Daghappen worden vergezeld door een aardappelgarnituur en een frisse salade.<br /> Daghap is exclusief bijpassende wijn.</p> <p> </p> <p><strong>Wij verwelkomen u graag!</strong></p> <p><em><strong>Team Bistro Puur</strong></em></p> <p class="rteindent1 rtecenter"><strong> </strong></p> an Bistro Puur';
$html = str_get_html($html); // create a html DOM object again
foreach($html->find('strong') as $e){ // search for <strong> tag
$temp[] = [$e->innertext]; // get the inside of the html tag
}
print_r($temp);
Array
(
[0] => <pre> </pre><p><strong>De heerlijke Pure Daghappen v</strong><strong>an Bistro Puur</strong></p> <p><strong>Voor de vaste lage prijs van slechts € 8,50 !</strong></p> <p>geserveerd tussen 17:00 uur & 18:30 uur</p> <p> </p> <h2> <strong>De heerlijke Pure Daghappen voor week 32 zijn:</strong></h2> <p><strong><u>Dinsdag 22 Augustus:</u></strong></p> <p><strong>Gegrilde vakenshaas medaillons met champignonsaus</strong></p> <p> <strong><u>Woensdag 23 Augustus:</u></strong> </p> <p><strong>Gekonfijte scharrelhoen bout met truffelsaus</strong></p> <p> <strong><u>Donderdag 24 Augustus: </u></strong> </p> <p> <br /> <strong>Gebakken zalmfilet met witte wijnsaus</strong></p> <p> <strong><u>Vrijdag 25 Augustus;</u></strong></p> <p><strong>In de oven gebakken kabeljauw filet met pesto saus</strong></p> <p> </p> <p>Alle heerlijke Pure Daghappen worden vergezeld door een aardappelgarnituur en een frisse salade.<br /> Daghap is exclusief bijpassende wijn.</p> <p> </p> <p><strong>Wij verwelkomen u graag!</strong></p> <p><em><strong>Team Bistro Puur</strong></em></p> <p class="rteindent1 rtecenter"><strong> </strong></p>
[1] => Array
(
[0] => De heerlijke Pure Daghappen v
)
[2] => Array
(
[0] => an Bistro Puur
)
[3] => Array
(
[0] => Voor de vaste lage prijs van slechts € 8,50 !
)
[4] => Array
(
[0] => De heerlijke Pure Daghappen voor week 32 zijn:
)
[5] => Array
(
[0] => <u>Dinsdag 22 Augustus:</u>
)
[6] => Array
(
[0] => Gegrilde vakenshaas medaillons met champignonsaus
)
[7] => Array
(
[0] => <u>Woensdag 23 Augustus:</u>
)
[8] => Array
(
[0] => Gekonfijte scharrelhoen bout met truffelsaus
)
[9] => Array
(
[0] => <u>Donderdag 24 Augustus: </u>
)
[10] => Array
(
[0] => Gebakken zalmfilet met witte wijnsaus
)
[11] => Array
(
[0] => <u>Vrijdag 25 Augustus;</u>
)
[12] => Array
(
[0] => In de oven gebakken kabeljauw filet met pesto saus
)
[13] => Array
(
[0] => Wij verwelkomen u graag!
)
[14] => Array
(
[0] => Team Bistro Puur
)
[15] => Array
(
[0] =>
)
)
Questions:
array[0]
? <u>..</ul>
tags? I updated to code to (based on the feedback from Peter):
$html = str_get_html($str);
foreach($html->find('strong') as $e){
$temp[] = $e->plaintext;
}
This gives me the correct plain array with all the data. Great!
Why am I getting the whole html string again at array[0]?
Probably because you still have the following line in your code, initializing the $temp
-variable with an array with a single element $e->innertext
.
$temp = [$e->innertext];
Initialize the $temp
variable just before your foreach
with $temp = [];
.
How can I quickly remove the remaining ,.. tags?
Use $e->plaintext
instead of $e->innertext
(make sure to read the documentation )
I am obtaining 15 nested arrays. I can work with those but is it possible to remove the nesting from the start?
Because you tell it to create an array everytime (why are you doing that??):
$temp[] = [ $e->innertext ];
^ array open ^ array-close
Use $temp[] = $e->innertext;
instead (or plaintext
according to previous)
Don't know how you are including html DOM, but using this script:
<?php
include "simple_html_dom.php";
$html = '</pre><pre> </pre><p><strong>De heerlijke Pure Daghappen v</strong><strong>an Bistro Puur<$
$html = str_get_html($html); // create a html DOM object again
foreach($html->find('strong') as $e){ // search for <strong> tag
$temp[] = $e->plaintext; // get the inside of the html tag
}
var_dump_pre($temp);
function var_dump_pre($mixed = null) {
echo '<pre>';
print_r($mixed);
echo '</pre>';
return null;
}
?>
...will produce this output:
Array
(
[0] => De heerlijke Pure Daghappen v
[1] => an Bistro Puur
[2] => Voor de vaste lage prijs van slechts € 8,50 !
[3] => De heerlijke Pure Daghappen voor week 32 zijn:
[4] => Dinsdag 22 Augustus:
[5] => Gegrilde vakenshaas medaillons met champignonsaus
[6] => Woensdag 23 Augustus:
[7] => Gekonfijte scharrelhoen bout met truffelsaus
[8] => Donderdag 24 Augustus:
[9] => Gebakken zalmfilet met witte wijnsaus
[10] => Vrijdag 25 Augustus;
[11] => In de oven gebakken kabeljauw filet met pesto saus
[12] => Wij verwelkomen u graag!
[13] => Team Bistro Puur
[14] =>
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.