Skip to content

Instantly share code, notes, and snippets.

@hbastidas
Created November 12, 2021 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hbastidas/eaaed67b104efbea010c9d87c100d825 to your computer and use it in GitHub Desktop.
Save hbastidas/eaaed67b104efbea010c9d87c100d825 to your computer and use it in GitHub Desktop.
example
const puppeteer = require('puppeteer');
(async () => {
const url = "https://www.google.com/search?q=arsenal&hl=en";
puppeteer.launch({devtools:false}).then(async function(browser){
//create a load_page function that returns a promise which resolves when screenshot is taken
async function load_page(url){
return new Promise(async function(resolve, reject){
const page = await browser.newPage();
console.log("ir a url "+url)
promesa = await Promise.all([
page.goto(url, {"waitUntil":["load", "networkidle2"]}),
new Promise(async function(resolve, reject){
//Close Popup Term Google
if( await page.waitForSelector('.VDity') !== null ) {
await page.evaluate(()=>{
let e = document.getElementsByClassName('VDity')[0].children[1].click();
return "ok";
});
}else {
console.log("NO existe popup");
}
await page.waitForSelector('#sports-app g-immersive-footer g-fab');
await page.click('#sports-app g-immersive-footer g-fab');
await page.waitForSelector('.liveresults-sports-immersive__match-grid');
await page.waitForTimeout(5000).then(() => console.log('Waited five seconds'));
//await page.waitForSelector('#lb div.imso-fpm');
await page.mouse.move(0, 0);
let matches =[];
matches = await page.evaluate(()=>{
var tables = document.getElementsByClassName('liveresults-sports-immersive__match-grid');
var results = [];
for(table in tables) {
if(typeof tables[table] == 'object') {
var matches = tables[table].getElementsByClassName('liveresults-sports-immersive__match-tile');
for(match in matches) {
if(typeof matches[match] == 'object' && matches[match].getElementsByTagName('tbody').length > 0) {
var competition = 'Unknown';
if(typeof matches[match].getElementsByTagName('tbody')[0].getElementsByTagName('tr')[1].getElementsByTagName('span')[0] != "undefined") {
competition = matches[match].getElementsByTagName('tbody')[0].getElementsByTagName('tr')[1].getElementsByTagName('span')[0].textContent
}
var dateTime = matches[match].getElementsByClassName('imso-loa')[0].getAttribute("data-start-time");
var homeTeam = '';
if( matches[match].getElementsByTagName('tbody')[0].getElementsByTagName('tr')[4].getElementsByTagName('td')[1].getElementsByClassName('ellipsisize')[0].getElementsByTagName('span')[0] != undefined ) {
homeTeam = matches[match].getElementsByTagName('tbody')[0].getElementsByTagName('tr')[4].getElementsByTagName('td')[1].getElementsByClassName('ellipsisize')[0].getElementsByTagName('span')[0].textContent;
}
var homeLogo = '';
if(matches[match].getElementsByTagName('tbody')[0].getElementsByTagName('tr')[4].getElementsByTagName('td')[0].getElementsByTagName('img')[0]) {
homeLogo = 'https:'+matches[match].getElementsByTagName('tbody')[0].getElementsByTagName('tr')[4].getElementsByTagName('td')[0].getElementsByTagName('img')[0].getAttribute("src");
}
var visitorTeam = '';
var visitorLogo = '';
if(matches[match].getElementsByTagName('tbody')[0].getElementsByTagName('tr')[5].getElementsByTagName('td')[1].getElementsByClassName('ellipsisize')[0].getElementsByTagName('span')[0]) {
visitorTeam = matches[match].getElementsByTagName('tbody')[0].getElementsByTagName('tr')[5].getElementsByTagName('td')[1].getElementsByClassName('ellipsisize')[0].getElementsByTagName('span')[0].textContent;
if(matches[match].getElementsByTagName('tbody')[0].getElementsByTagName('tr')[5].getElementsByTagName('td')[0].getElementsByTagName('img')[0]) {
visitorLogo = 'https:'+matches[match].getElementsByTagName('tbody')[0].getElementsByTagName('tr')[5].getElementsByTagName('td')[0].getElementsByTagName('img')[0].getAttribute("src");
}
}
var homeScore = null;
if(typeof matches[match].getElementsByTagName('tbody')[0].getElementsByTagName('tr')[4].getElementsByTagName('td')[1].getElementsByClassName('imspo_mt__t-sc')[0] != "undefined") {
homeScore = matches[match].getElementsByTagName('tbody')[0].getElementsByTagName('tr')[4].getElementsByTagName('td')[1].getElementsByClassName('imspo_mt__t-sc')[0].getElementsByTagName('div')[0].textContent;
}
var visitorScore = null;
if(typeof matches[match].getElementsByTagName('tbody')[0].getElementsByTagName('tr')[5].getElementsByTagName('td')[1].getElementsByClassName('imspo_mt__t-sc')[0] != "undefined") {
visitorScore = matches[match].getElementsByTagName('tbody')[0].getElementsByTagName('tr')[5].getElementsByTagName('td')[1].getElementsByClassName('imspo_mt__t-sc')[0].getElementsByTagName('div')[0].textContent;
}
var status_match='';
if(typeof matches[match].getElementsByTagName('tbody')[0].getElementsByTagName('tr')[2].getElementsByTagName('td')[2].getElementsByClassName('imspo_mt__ms-w')[0].getElementsByTagName('div')[0].getElementsByTagName('div')[0].getElementsByTagName('div')[0] != "undefined") {
status_match = matches[match].getElementsByTagName('tbody')[0].getElementsByTagName('tr')[2].getElementsByTagName('td')[2].getElementsByClassName('imspo_mt__ms-w')[0].getElementsByTagName('div')[0].getElementsByTagName('div')[0].getElementsByTagName('div')[0].textContent;
}
results[results.length] = {
'competition' : competition,
'local' : homeTeam,
'visitor' : visitorTeam,
'local_score' : homeScore,
'visitor_score' : visitorScore,
'local_logo' : homeLogo,
'visitor_logo' : visitorLogo,
'date' : dateTime.split('T')[0],
'time' : dateTime.split('T')[1].replace('Z',''),
'status_match' : status_match
};
}
}
}
}
return results;
});
resolve(matches);
})
]);
await page.close();
resolve(promesa);
})
}
//step through the list of papers, calling the above load_page()
let data=await load_page(url);
await browser.close();
console.log(data);
})
// const browser = await puppeteer.launch({headless: false});
// const page = await browser.newPage();
// await page.goto('http://whatsmyuseragent.org/');
// await page.screenshot({ path: `example.png` });
// await browser.close();
})();
@hbastidas
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment