Created
September 4, 2018 08:38
-
-
Save zawataki/ba18d6e21c9322c5afba3a843fe0fb58 to your computer and use it in GitHub Desktop.
Scrape team info
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript: ( | |
function () { | |
function objectListToCsv(objList) { | |
let csvMap = {}; | |
for (let index = 0; index < objList.length; index++) { | |
const item = objList[index]; | |
for (const key in item) { | |
if (!csvMap.hasOwnProperty(key)) { | |
csvMap[key] = []; | |
} | |
csvMap[key][index] = item[key]; | |
} | |
} | |
console.debug('csvMap:', csvMap); | |
const NEW_LINE = '\n'; | |
const DELIMITER = ','; | |
const QUOTE = '"'; | |
const csvKeys = Object.keys(csvMap).sort(); | |
let csv = csvKeys.join(DELIMITER) + NEW_LINE; | |
for (let index = 0; index < objList.length; index++) { | |
csv += csvKeys.map(key => csvMap[key][index]) | |
.map(value => QUOTE + (value ? value : '') + QUOTE) | |
.join(DELIMITER) + NEW_LINE; | |
} | |
return csv; | |
} | |
/** | |
* @param {String} textVal A text to copy to clipboard | |
* @returns {boolean} true if succeeded. Otherwise false | |
*/ | |
function copyTextToClipboard(textVal) { | |
let copyFrom = document.createElement("textarea"); | |
copyFrom.textContent = textVal; | |
let bodyElm = document.getElementsByTagName("body")[0]; | |
bodyElm.appendChild(copyFrom); | |
copyFrom.select(); | |
let retVal = document.execCommand('copy'); | |
bodyElm.removeChild(copyFrom); | |
return retVal; | |
} | |
const targetElements = $('div#main > h3,div#main > ul'); | |
let teams = []; | |
let team = {}; | |
targetElements.each(function () { | |
if (this.tagName === 'H3') { | |
if (Object.values(team).length !== 0) { | |
console.debug('team:', team); | |
teams.push(team); | |
} | |
team = {}; | |
team.name = this.innerText; | |
} else { | |
let liInnerTextList = Array.prototype.map.call(this.children, function (item) { | |
return item.innerText; | |
}); | |
console.debug('liInnerTextList:', liInnerTextList); | |
for (const str of liInnerTextList) { | |
let trimmedStr = String(str).trim(); | |
let matchResult = trimmedStr.match(/^([^\p{White_Space}]+)\p{White_Space}+(.*)$/u); | |
let key = matchResult[1]; | |
let value = matchResult[2]; | |
if (key === '会員') { | |
console.info(key + ': ' + value); | |
const REGEXP = /^(男性)(\d+)名\p{White_Space}+(女性)(\d+)名$/u; | |
console.info('REGEXP: ' + REGEXP); | |
let result = value.trim().match(REGEXP); | |
console.info('result:', result); | |
if (result) { | |
let numberOfMale = result[2]; | |
let numberOfFemale = result[4]; | |
team[key + ' ' + result[1]] = numberOfMale; | |
team[key + ' ' + result[3]] = numberOfFemale; | |
} else { | |
team[key] = value; | |
} | |
} else { | |
team[key] = value; | |
} | |
} | |
} | |
}); | |
if (Object.values(team).length !== 0) { | |
console.debug('team:', team); | |
teams.push(team); | |
} | |
console.info('teams:', teams); | |
const csv = objectListToCsv(teams); | |
console.debug(csv); | |
copyTextToClipboard(csv); | |
alert('Copied csv text to clipboard'); | |
} | |
)(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment