PHP Code for Web / Content Scraping

Hi,

Today I am happy to share you a PHP code snippet / sample which scraps web content from website's far more faster then you think. Although this is just a sample, but you can code a awesome extension for your website or system which reads multiple website's in no time for new and better content.


Simple PHP Scraper
PHP has a DOMXpath function. I’m not going to explain how this function works, but with the script below you can easily scrape a list of URLs. Since it is PHP, use a cronjob to hourly, daily or weekly scrape the desired data. If you are not used to creating Xpath references, use Chrome plugin by selecting the data point and see the Xpath reference directly.

<?php
error_reporting(0);

$arr = array('URL_LINKS_GOES_HERE1','URL_LINKS_GOES_HERE2'); // insert list of URLs to scrape

echo "<table>";
foreach ($arr as &$value) {

$file = $DOCUMENT_ROOT. $value;
$doc = new DOMDocument();
$doc->loadHTMLFile($file);
$xpath = new DOMXpath($doc);

$elements = $xpath->query("XPATH_OF_CONTENT_ELEMENT"); // insert Xpath reference.
    
if (!is_null($elements)) {
 echo "<tr>";
 echo "<td>".$value."</td>";
  foreach ($elements as $element) {
 
 $nodes = $element->childNodes;
    foreach ($nodes as $node) {
      echo "<td>".$node->nodeValue. "</td>\n";
    }
 
  }
  echo "</tr>";
}

}
echo "</table>";
?>



Find the Nearest Number which ends with zero in JAVA

Simplest Code:

// round it off to zero - Number ending from 1 to 9 will become zero.

int MyNumber = new Double(MyNumber / 10).intValue();
MyNumber = MyNumber * 10;

// Compile and Run to Test it out

public class toZero{

     public static void main(String []args){
        int MyNumber = 1556;      
       
        MyNumber = new Double(MyNumber / 10).intValue();
        MyNumber = MyNumber * 10;

        System.out.println(MyNumber);
     }
}

Enable Column Sorting in a jtable - java

Easiest way to enable sorting in a jtable in desktop java is write line of code

jTableObject.setAutoCreateRowSorter(true);

JSON and JAVASCRIPT differences and code difference

Javascript Array: 

var obj = []; 
obj.push = 'Some VALUE'; Or var obj = ['SomeVALUE', 1, 2]; 
now to access them use obj[0] -- 0 stands the index No. 
*********************************************** 

JSON Array and manipulations: 

Example 
var obj = { name : value, anotherName : anotherValue };  // if JS Variable used
var obj = { 'name' : 'value', 'anotherName' : 'anotherValue' }; // if JS Strings are used
Or Else
 ips[ipId] = {}; 
ips[ipId]['name'] = value; 
ips[ipId]['anotherName'] = anotherValue;
now - 
JSON.stringify(object); // gets you the string of the JSON Object
// No need to parse, no need to use JSON.parse as the variable holds the JSON Object only.
if you want to create a JSON Object from string or reverse engineer JSON.stringify() Method then use JSON.parse();

Also, You can use string variables to specify the names of the properties:
var name = 'name'; // name is variable - changing its value often
obj[name] = value; // note that their are no single quotes near name variable;
name = 'anotherName';
obj[name] = anotherValue;a