Skip to content

Instantly share code, notes, and snippets.

@WillsB3
Last active January 20, 2023 21:11
Show Gist options
  • Save WillsB3/387a82b8d95c197029bf to your computer and use it in GitHub Desktop.
Save WillsB3/387a82b8d95c197029bf to your computer and use it in GitHub Desktop.
USwitch Mobile Phone Data Scraper Snippet

USwitch Mobile Phone Data Scraper Snippet

Description

Find the best Pay Montly deals for the iPhone 6SE in the UK. See an example sheet created using this script on http://www.uswitch.com/mobiles/deals/apple_iphone_se_64gb_rose_gold/ for the 64GB Rose Gold 6SE: https://docs.google.com/spreadsheets/d/1WICZ8bqps4Pwb-QRcLr7scEP5zd7CgtYTeRF82hZflw/edit#gid=883967046.

How to use:

  1. Go to a USwitch search results page (such as http://www.uswitch.com/mobiles/deals/apple_iphone_se_64gb_rose_gold/). Click "Load More" until no more results are offered.
  2. Add this Javascript as a Dev Tools snippet, or just copy & paste it into your Dev Tools console and execute it to get a the data in a tab separated string.
  3. This data can then be copyied into a Google Doc where you can apply filtering/sorting as you require.

TODO:

Automate step 1.

var parsedDeals = [];
var csv = '';
var dealRows = $('.table-row');
var fields = ['network', 'duration', 'monthly', 'upfront', 'data', 'mins', 'texts', 'cashback', 'url'];
fields.forEach(function(f, i, a) {
csv += f + '\t';
});
csv += '\n';
$.each(dealRows, function(idx, deal) {
var $d = $(deal);
var d = {};
var a;
var upfront = $d.find('[data-bind="html: handset_cost, visible: (handset_cost != \'FREE\')"]').eq(0).text();
if (upfront === 'FREE') {
upfront = '£0';
} else {
upfront = upfront.replace(' upfront', '');
}
d.network = $d.find('.table-image__network img').get(0).alt;
d.duration = $d.find('span[data-bind="html: contract_length"]').text().replace(' month', '');
d.monthly = $d.find('.monthly-cost__title').eq(0).text();
d.upfront = upfront;
d.data = $d.find('[data-bind="html: data"]').eq(0).text();
d.mins = $d.find('[data-bind="html: minutes"]').eq(0).text();
d.texts = $d.find('[data-bind="html: texts"]').eq(0).text();
a = $('<a>', { href: $d.find('a').attr('href') })[0];
d.url = a.href;
d.cashback = $d.find('span.cashback').not('.usp').text().replace(' cashback', '');
parsedDeals.push(d);
var csvRow = '';
fields.forEach(function(f, i, a) {
csvRow += d[f];
if (i < a.length - 1) {
csvRow += '\t';
}
});
csv += csvRow + '\n';
});
console.table(parsedDeals);
console.table(csv);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment