-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.js
More file actions
80 lines (52 loc) · 2.19 KB
/
scraper.js
File metadata and controls
80 lines (52 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* PHPScraper - Webpage Data Scraper Ajax.
* This file is part of the PHPScraper package.
*
* @version 1.0
* @copyright Copyright © 2016 Sebastian Inman (http://sebastianinman.com)
* @author Sebastian Inman <sebastian.inman.design@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
;(function($, undefined) {
'use strict';
/**
* Scrape the webpage for meta data.
*
* @param (array) queries // array of desired meta tags. fallback fetches all available meta data
* @param (array) websites // array of websites to be scraped by the meta fetcher
* @param (function) callback // function fires after all requested data is returned
*/
$.fn.scrapeWebsite = function(queries, websites, callback) {
// Begin the query string.
var query = '&queries=';
// Check if any queries have been passed into the function.
if(queries && queries.length) {
// There was at least one query passed into the function.
// Begin looping through each query.
$.each(queries, function(i, string) {
// Add each query to the pre-defined query string.
// This query string will be passed to the PHP scraper via Ajax.
query += i >= queries.length - 1 ? string : string + ',';
});
}
// Begin looping through each website passed into the function.
$.each(websites, function(key, website) {
// Create an ajax call for the current website in the array.
// We'll be sending all the query data along with it to tell the
// PHP function exacly what data we want to scrape from the webpage.
// If no queries have been passed, the function defaults to returning
// all available meta data for the webpage.
$.ajax({
dataType: 'json', // return data as jSon
url: 'scraper-ajax.php?url=' + website + query, // pass the query
success: function(response) {
// The Ajax call was successful and we now have data available.
// Fire the callback function if it is, in fact, a function.
if(callback && typeof callback == 'function') {
callback(response);
}
}
});
});
};
})(jQuery);