title: Simple Web scraping PHP Xpath DOM
body: <p>I´m trying to learn web scraping and using this example to get the links from a page. Is there some better way to do it, or for example what is the easiest way to get the h1? </p>
  
  <pre><code>$html = file_get_contents('page.html');
  
  //parse the html into a DOMDocument
  $dom = new DOMDocument();
  @$dom-&gt;loadHTML($html);
  
  //grab all the links on the page
  $xpath = new DOMXPath($dom);
  $hrefs = $xpath-&gt;evaluate("/html/body//a");
  
  for ($i = 0; $i &lt; $hrefs-&gt;length; $i++) {
      $href = $hrefs-&gt;item($i);
      $url = $href-&gt;getAttribute('href');
      echo "&lt;br /&gt;Link: $url";
  
  }
  </code></pre>