I have working scraper here but im struggling with sorting the data.
Here is the scraper code:
var http = require('http');
var request = require('request');
var cheerio = require('cheerio');
http.createServer(function (req, res) {
request('http://www.xscores.com/soccer', function (error, response,
html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
var list_items = "";
var arr = [];
var arr2 = [];
var j = 1;
$('div.match_line.score_row.other_match.e_true').each(function (i, element) {
var a = $(this).attr('data-home-team');
arr.push(a + " Row Number " + j);
j = j + 2;
//list_items += "<li>" + a + "</li>";
//console.log(arr.length);
});
var j = 2;
$('div.match_line.score_row.other_match.o_true').each(function (i, element) {
var b = $(this).attr('data-home-team');
arr.push(b + " Row Number " + j);
j = j + 2;
//list_items += "<li>" + b + "</li>";
//console.log(arr.length);
});
var arrayLength = arr.length;
for (var i = 0; i < arrayLength; i++) {
list_items += "<li>" + arr[i] + "</li>";
}
var html = "<ul>" + list_items + "</ul>"
res.writeHead(200, {
'Content-Type': 'text/html'
});
res.end(html);
console.log(arr.length);
}
});
}).listen(8080);
console.log('Server is running at http://178.62.253.206:8080/');
So to the problem, Sorting these elements.
Element 'div.match_line.score_row.other_match.e_true' are in placed every nth
Odd Row Number in the source page.
And element 'div.match_line.score_row.other_match.o_true' are in placed every nth Even Row Number in the source page.
The way It gets sorted now
My array(arr) first pushes all rows of data into the array from the first
element (odd row number)
Then array(arr) pushes all rows of data from the 2nd element into the same
array. (even row number)
So basically items in my array is sorted like this 1,3,5,7,9,2,4,6,8 (shortened)
How can I sort this so I can output this in correct order ?
Any suggestion for a solution would be much appreciated
Frederik
Check the arr.sort method below.
var http = require('http');
var request = require('request');
var cheerio = require('cheerio');
http.createServer(function(req, res) {
request('http://www.xscores.com/soccer', function(error, response,
html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
var list_items = "";
var arr = [];
var arr2 = [];
var j = 1;
$('div.match_line.score_row.other_match.e_true').each(function(i, element) {
var a = $(this).attr('data-home-team');
arr.push({
html: a,
j: j
});
j = j + 2;
//list_items += "<li>" + a + "</li>";
//console.log(arr.length);
});
var j = 2;
$('div.match_line.score_row.other_match.o_true').each(function(i, element) {
var b = $(this).attr('data-home-team');
arr.push({
html: b,
j: j
});
j = j + 2;
//list_items += "<li>" + b + "</li>";
//console.log(arr.length);
});
arr.sort(function(a, b) {
return a.j - b.j
})
var arrayLength = arr.length;
for (var i = 0; i < arrayLength; i++) {
list_items += "<li>" + arr[i].html + "</li>";
}
var html = "<ul>" + list_items + "</ul>"
res.writeHead(200, {
'Content-Type': 'text/html'
});
res.end(html);
console.log(arr.length);
}
});
}).listen(8080);
console.log('Server is running at http://178.62.253.206:8080/');
Related
I am trying to access children data from a Json response to use it in an if statement but i don't know-how. Anyone know how to do this?
Here is the screenshot of the response and I have circled the object I want to access. I want only the summation to happen if the approval has a value i.e. status needs to be pending or approved, otherwise, no calculation will happen.
Here is the code that I use but don't know how to access the approaval={data=[{status}] form the JSON so as to use it
function showTimeData() {
var users = getUsers()
var endpoint = 'users/';
var time_array = [];
for (var i = 0; i < users.length; i++) {
var url = 'https://api.10000ft.com/api/v1/users/' + users[i].id + '/time_entries?fields=approvals' + '&from=' + from + '&to=' + to + '&auth=' + TKF_AUTH;
var response = UrlFetchApp.fetch(url, options);
var info = JSON.parse(response.getContentText());
var content = info.data;
var total_hours = 0;
for (var j = 0; j < content.length; j++) {
if (content.data.approvals.data.length > 0) {
hoursTotal = 0;
}
total_hours += parseInt(content[j].hours);
}
Logger.log('User name: ' + users[i].display_name + ' ' + 'User id: ' + users[i].id + ' ' + 'total hours: ' + total_hours)
}
First of all. You need to fix the error(mentioned in the chat in comments):
Replace this
if (content.data.approvals.data.length > 0) {
hoursTotal = 0;
}
with this
if (content[j].approvals.data.length > 0) {
hoursTotal = 0;
}
Then what you need is:
content[0].approvals.data[0].status
or an array of statuses:
content[0].approvals.data.map(el => el.status)
or an array of all statuses:
content.map(el => el.approvals.data.map(it => it.status)).flat(1)
but last example will work only in fairly new browsers.
The following code works only if the table is already present in the document upon page load. I however want it to apply on a dynamically created table.
Can this be done?
var colNumber=22
for (var i=0; i<colNumber; i++)
{
var thWidth=$("#tbl").find("th:eq("+i+")").width();
var tdWidth=$("#tbl").find("td:eq("+i+")").width();
if (thWidth<tdWidth)
$("#tbl").find("th:eq("+i+")").width(tdWidth);
else
$("#tbl").find("td:eq("+i+")").width(thWidth);
}
The table is created in the following way:
function loadFile(event){
alasql('SELECT * FROM FILE(?,{headers:false})',[event],function(data){
var keys = [];
for (var i = 0; i < data.length; i++) {
for (var categoryid in data[i]) {
var category = data[i][categoryid];
keys.push(categoryid);
}
}
keysConverted = keys.map(foo);
var vMin = Math.min.apply(null, keysConverted);
var vMax = Math.max.apply(null, keysConverted);
var start = vMin-1
var ColNeeded = vMax - vMin+1;
var arrExcel2Table = '<table id="tbl">';
for (var i = 0; i < data.length; i++){
arrExcel2Table = arrExcel2Table + '<tr>';
for (var j = 0; j < ColNeeded; j++){
cellValue = data[i][number2Letter(j+start)];
if (typeof cellValue === "undefined"){
cellValue = '';
}
arrExcel2Table = arrExcel2Table + '<td>' + cellValue + '</td>';
}
arrExcel2Table = arrExcel2Table + '</tr>';
}
arrExcel2Table = arrExcel2Table + '</table>';
document.getElementById('excel_table').innerHTML = arrExcel2Table;
});
}
Create a function you want to run and add an event from the dynamic element. For example
arrExcel2Table = arrExcel2Table + '<td>' + cellValue + '</td>';
can be replaced by
arrExcel2Table = arrExcel2Table + '<td onclick="myFunction();">' + cellValue + '</td>';
Above code will call the function you created
myFunction() {
alert();
}
Just create your table, then apply whatever code you want to it :
$('#excel_table').html(arrExcel2Table);
adjustWidth()
function adjustWidth(){
var $tbl = $("#tbl"); // And cache your jQuery objects!! Massive performance boost
for (var i=0; i<colNumber; i++)
{
var $th = $tbl.find("th:eq("+i+")"),
$td = $tbl.find("td:eq("+i+")"),
thWidth = $th.width(),
tdWidth = $td.width();
if (thWidth<tdWidth)
$th.width(tdWidth);
else
$td.width(thWidth);
}
}
how do i include json data in the load more button function at
load more button : Jsfiddle example
var index = 1;
$(document).on("pagecreate", "#page1", function(){
$("#btnAdd").on("click", function(){
var allItems = '';
for (var i = 0; i < 100; i++) {
allItems += '<li><a href="javascript:showDetails(' + (index + i) + ')" >item number ' + (index + i) + '</a></li>';
}
index += 100;
$("#ListTemp").empty().append(allItems).listview("refresh");
var element = $("#ListTemp li").detach();
$("#ListDspQry").append(element);
});
});
My json url is : http://www.beritanisma.com/category/superlawak/?json=get_category_posts
been trying for days but it ended up showing no results, please help me masters, thank you..
var index = 0;
$(document).on("pagecreate", "#page1", function(){
$("#btnAdd").on("click", function(){
var url = "http://www.beritanisma.com/category/superlawak/?json=get_category_posts"
$.getJSON(url, function(data){
var allItems = '';
for (var i = 0; i < data.posts.length; i++) {
console.log(data.posts[i].url);
allItems += '<li><a href="javascript:showDetails(' + (index + i) + ')" >'+data.posts[i].url+'' + (index + i) + '</a></li>';
}
index += 100;
$("#ListTemp").empty().append(allItems).listview("refresh");
var element = $("#ListTemp li").detach();
$("#ListDspQry").append(element);
});
});
});
I'm defining the following action to happen when pressing a button on my HTML:
$(document).ready(function() {
$("#query").keydown(function () {
// stuff
$.get(url, function (result) {
console.log(result);
var list = "";
for (var i = 0, l = result["results"].length; i < l; i++) {
list += '<li>' + result["results"][i]["label"] + '</li>';
}
list = "Here are some results: <ul>" + list + "</ul>";
});
});
What arrives in "result" is a JSON array in the following form:
{"results":[{"label":"something"},{"label":"something else"},{"label":"many other ones"}]}
So, why is my reference to length being interpreted as a reference to the property of a null value?
I believe you are getting a json input. You forgot to convert the json into an actual Javascript Object. You can do so with one of the two following ways.
$(document).ready(function() {
$("#query").keydown(function () {
// stuff
$.get(url, function (result) {
result = JSON.parse(result);
console.log(result);
var list = "";
for (var i = 0, l = result["results"].length; i < l; i++) {
list += '<li>' + result["results"][i]["label"] + '</li>';
}
list = "Here are some results: <ul>" + list + "</ul>";
});
Or
$(document).ready(function() {
$("#query").keydown(function () {
// stuff
$.getJSON(url, function (result) {
console.log(result);
var list = "";
for (var i = 0, l = result["results"].length; i < l; i++) {
list += '<li>' + result["results"][i]["label"] + '</li>';
}
list = "Here are some results: <ul>" + list + "</ul>";
});
I have a phonegap db functioning JS script below. I want to return the final string variable "feeds" outside the whole function. This only returns "undefined". Please help me with required changes to return the "feeds" variable.`
function getProviders() {
var feeds = "";
var db = window.openDatabase("db", "1.0", "desc", 1000000);
db.transaction(function(tx) {
var db = window.openDatabase("db", "1.0", "desc", 1000000);
tx.executeSql("SELECT * FROM `feed_provider`", [], function(tx, results) {
var len = results.rows.length;
for (var i = 0; i < len; i++) {
feeds += results.rows.item(i).id + "|" + results.rows.item(i).name + "|" + results.rows.item(i).status + "|" + results.rows.item(i).feed_url + ",";
}
}, sqlerror);
}, sqlerror2);
return feeds;
}
I'm going to assume either db.transaction or tx.executeSql is async, in which case I would use a deferred:
function getProviders() {
var feeds = "";
var def = $.Deferred();
var db = window.openDatabase("db", "1.0", "desc", 1000000);
db.transaction(function(tx) {
var db = window.openDatabase("db", "1.0", "desc", 1000000);
tx.executeSql("SELECT * FROM `feed_provider`", [], function(tx, results) {
var len = results.rows.length;
for (var i = 0; i < len; i++) {
feeds += results.rows.item(i).id + "|" + results.rows.item(i).name + "|" + results.rows.item(i).status + "|" + results.rows.item(i).feed_url + ",";
}
def.resolve(feeds);
}, sqlerror);
}, sqlerror2);
return def.promise();
}
Call it like this:
getProviders().done(function(feeds) {
// do something with feeds
});