hello dear community,

i want to get the results of a simple DOM parser-object - sorted in a better manner - cvs or so

see here the simple DOM program to extract Google result links eg for this URL

https://www.google.de/search?q=wordpress+coding+tutorial&ie=utf-8&oe=utf-8&client=firefox-b&gfe_rd=cr&dcr=0&ei=4JsOWruLGsnBXouFlqAL

<?php

# Use the Curl extension to query Google and get back a page of results
$url = "my google request ";
$ch = curl_init();
$timeout = 5;
# sets the timeout 
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$html = curl_exec($ch);
curl_close($ch);

# Create a DOM parser object
$dom = new DOMDocument();

# Parse the HTML from Google.
# The @ before the method call suppresses any warnings that
# loadHTML might throw because of invalid HTML in the page.
@$dom->loadHTML($html);

# Iterate over all the <a> tags
foreach($dom->getElementsByTagName('a') as $link) {
        # Show the <a href>
        echo $link->getAttribute('href');
        echo "<br />";
}
?>


i get a bunch of urls

what i want to achieve that the result is coming in a better overview - eg. as a cvs output or so?

    So, don't "echo" the output.

    Instead, put the hrefs in an array and then output them in "cvs" (or did you mean CSV?), or HTML or XML or Excel or a PDF or JSON or ...

    By the way, experience suggests that you won't be able to get very many links from Google with a parser before they shut you down with RECAPTCHA....

      hello dear dalecosp,

      first of all - many many thanks for the hint. i appreciate your help.

      dalecosp;11064801 wrote:

      So, don't "echo" the output.

      Instead, put the hrefs in an array and then output them in "cvs" (or did you mean CSV?), or HTML or XML or Excel or a PDF or JSON or ...

      By the way, experience suggests that you won't be able to get very many links from Google with a parser before they shut you down with RECAPTCHA....

      i will look for the array ; i put the results in an array and try to get an output in CSV or JSON

      i come back and report all the findings

      greetings

        If you have it in an array, then it should hopefully be pretty simple:

        • Use header() to set the applicable Content-Type HTTP header.
        • Either use json_encode() to convert the array to JSON, then simply output that and exit, or you can use fputcsv() with a foreach loop (see http://php.net/manual/en/function.fputcsv.php#72428 for how you can make the "file" handle actually just be direct output)

          hello again

          did some research ... . - well i think that this will help

          
          $rowData = array();
          
          foreach($table->find('tr') as $row) {
              // initialize array to store the cell data from each row
              $results = array();
              foreach($row->find('a href') as $cell) {
                  // push the cell's text to the array
                  $results[] = $cell->plaintext;
              }
              $rowData[] = $results;
          }
          
          echo '<table>';
          foreach ($rowData as $row => $tr) {
              echo '<tr>'; 
              foreach ($tr as $td)
                  echo '<td>' . $td .'</td>';
              echo '</tr>';
          }
          echo '</table>';
          

          i will do some trials...

            Write a Reply...