Advertisement
Guest User

fck

a guest
Nov 2nd, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.14 KB | None | 0 0
  1. <?php
  2.  
  3. const MYSQL_HOST = 'localhost';
  4. const MYSQL_USER = 'root';
  5. const MYSQL_PASSWORD = '123456789';
  6. const MYSQL_DATABASE = 'scraper';
  7.  
  8. const SCRAPER_NAME_INDEX = 22;
  9. const SCRAPER_PRICE_INDEX = 23;
  10. const SCRAPER_DESCRIPTION_INDEX = 25;
  11.  
  12. function scraper($url) {
  13.     $doc = new DOMDocument();
  14.     @$doc->loadHTML(file_get_contents($url));
  15.     $divs = $doc->getElementsByTagName('div');
  16.  
  17.     if (
  18.         !isset($divs[SCRAPER_NAME_INDEX], $divs[SCRAPER_PRICE_INDEX], $divs[SCRAPER_DESCRIPTION_INDEX])
  19.         || !preg_match('/eurostar.hu\/list2\.php/', $url)
  20.     ) {
  21.         echo 'Given site doesn\'t contains product details, try with an another URL.';
  22.         exit;
  23.     }
  24.  
  25.     echo 'Product name: ' . $divs[SCRAPER_NAME_INDEX]->nodeValue . '<br>';
  26.     echo 'Product price: ' . $divs[SCRAPER_PRICE_INDEX]->nodeValue . '<br>';
  27.     echo 'Product description: ' . $divs[SCRAPER_DESCRIPTION_INDEX]->nodeValue . '<br><hr>';
  28.  
  29.     save([
  30.         'name' => $divs[SCRAPER_NAME_INDEX]->nodeValue,
  31.         'price' => $divs[SCRAPER_PRICE_INDEX]->nodeValue,
  32.         'description' => $divs[SCRAPER_DESCRIPTION_INDEX]->nodeValue,
  33.     ]);
  34. }
  35.  
  36. function save(array $data) {
  37.     if (!$connection = connect()) {
  38.         die('Problem with MySQL connection. Please try again.');
  39.     }
  40.  
  41.     mysql_query(
  42.         sprintf(
  43.             'INSERT INTO products (name, price, description) VALUES ("%s", "%s", "%s")',
  44.             $data['name'],
  45.             $data['price'],
  46.             $data['description']
  47.         ),
  48.         $connection
  49.     );
  50.  
  51.     mysql_close($connection);
  52. }
  53.  
  54. function connect() {
  55.     if (!$connection = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password')) {
  56.         die('Could not connect: ' . mysql_error());
  57.     }
  58.  
  59.     return $connection;
  60. }
  61.  
  62. $url = '';
  63. if (isset($_POST['url'])) {
  64.     $url = $_POST['url'];
  65.     scraper($url);
  66. }
  67.  
  68. ?>
  69.  
  70. <html>
  71. <head>
  72.     <title>Custom Scraper</title>
  73. </head>
  74. <body>
  75. <h1>Custom Scraper</h1>
  76. <form method="POST">
  77.     <label>
  78.         URL:
  79.         <input type="url" value="<?=$url?>" name="url">
  80.     </label>
  81.     <button type="submit">Save</button>
  82. </form>
  83. </body>
  84. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement