0

The site i need to scrape is having structure like

<span class="address">
<p>...</p>
<h4>...</h4>
....
</span>

All i need is html inside of

<span class="address"></span>

What i am using is a code from google.

$html = new DOMDocument();
@$html->loadHtmlFile('www.site.com');
$xpath = new DOMXPath( $html );
$nodelist = $xpath->query( '//*[@id="main_center"]/div/div/div[2]/div/span[15]/p[6]' );

foreach ($nodelist as $n){
 echo $n->nodeValue."\n";
}

Its only giving me content without html,i need have all html so that i can filter them according to my needs.

Please provide suggestion, Thank you.

1
  • Anybody who can help me ? Commented Jul 11, 2016 at 8:04

1 Answer 1

2

try

<?php

  $html = new DOMDocument();
  @$html->loadHtmlFile('http://php.net/manual/de/domdocument.savehtml.php');
  $xpath = new DOMXPath( $html );
  $nodelist = $xpath->query( '//footer' );

  foreach ($nodelist as $n){
    echo $html->saveHtml($n)."\n";
  }

see: http://php.net/manual/en/domdocument.savehtml.php This is tested and works fine.

Do you outout direct to a Browser? Then look at the generated source code, the html tags will not be displayed in a brwoser ...

hth oli

Sign up to request clarification or add additional context in comments.

3 Comments

could you please tell me about what xpath i need to use there in my code also, because i need all contents of html which is having <span class="address">
o.k i have used //*[@class='address'], that is correct xpath, but your above code still giving me only contents, not with html.
htmlentities also works, but your first answer gave me what i needed most. Thank you very much Oliver, God bless you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.