Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- const MYSQL_HOST = 'localhost';
- const MYSQL_USER = 'root';
- const MYSQL_PASSWORD = '123456789';
- const MYSQL_DATABASE = 'scraper';
- const SCRAPER_NAME_INDEX = 22;
- const SCRAPER_PRICE_INDEX = 23;
- const SCRAPER_DESCRIPTION_INDEX = 25;
- function scraper($url) {
- $doc = new DOMDocument();
- @$doc->loadHTML(file_get_contents($url));
- $divs = $doc->getElementsByTagName('div');
- if (
- !isset($divs[SCRAPER_NAME_INDEX], $divs[SCRAPER_PRICE_INDEX], $divs[SCRAPER_DESCRIPTION_INDEX])
- || !preg_match('/eurostar.hu\/list2\.php/', $url)
- ) {
- echo 'Given site doesn\'t contains product details, try with an another URL.';
- exit;
- }
- echo 'Product name: ' . $divs[SCRAPER_NAME_INDEX]->nodeValue . '<br>';
- echo 'Product price: ' . $divs[SCRAPER_PRICE_INDEX]->nodeValue . '<br>';
- echo 'Product description: ' . $divs[SCRAPER_DESCRIPTION_INDEX]->nodeValue . '<br><hr>';
- save([
- 'name' => $divs[SCRAPER_NAME_INDEX]->nodeValue,
- 'price' => $divs[SCRAPER_PRICE_INDEX]->nodeValue,
- 'description' => $divs[SCRAPER_DESCRIPTION_INDEX]->nodeValue,
- ]);
- }
- function save(array $data) {
- if (!$connection = connect()) {
- die('Problem with MySQL connection. Please try again.');
- }
- mysql_query(
- sprintf(
- 'INSERT INTO products (name, price, description) VALUES ("%s", "%s", "%s")',
- $data['name'],
- $data['price'],
- $data['description']
- ),
- $connection
- );
- mysql_close($connection);
- }
- function connect() {
- if (!$connection = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password')) {
- die('Could not connect: ' . mysql_error());
- }
- return $connection;
- }
- $url = '';
- if (isset($_POST['url'])) {
- $url = $_POST['url'];
- scraper($url);
- }
- ?>
- <html>
- <head>
- <title>Custom Scraper</title>
- </head>
- <body>
- <h1>Custom Scraper</h1>
- <form method="POST">
- <label>
- URL:
- <input type="url" value="<?=$url?>" name="url">
- </label>
- <button type="submit">Save</button>
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement