Created
June 27, 2023 15:58
-
-
Save onimenotsuki/b05ba6aab3c119943cb39b70fa86b820 to your computer and use it in GitHub Desktop.
Download file with pupeteer
This file contains hidden or 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
import puppeteer from 'puppeteer'; | |
import path from 'path'; | |
(async () => { | |
const browser = await puppeteer.launch({ headless: false }); | |
const page = await browser.newPage(); | |
const client = await page.target().createCDPSession(); | |
await client.send('Page.setDownloadBehavior', { | |
behavior: 'allow', | |
downloadPath: path.join(__dirname + '/tmp'), | |
}); | |
await page.goto( | |
'https://sns.ift.org.mx:8081/sns-frontend/planes-numeracion/descarga-publica.xhtml', | |
{ waitUntil: 'networkidle0' }, | |
); | |
const tr_elements = await page.$x( | |
'html/body/div[1]/div[3]/div[2]/div[2]/div[5]/div[1]/table[2]/tbody/tr', | |
); | |
for (let i = 1; i <= tr_elements.length; i++) { | |
const text = await tr_elements[i].evaluate((el) => el.textContent); | |
if (text.toLocaleLowerCase().includes('doc')) { | |
try { | |
await page.click( | |
`#backtoTop > div.box_710_cap > div.box_710.box_white.box_710_white > div.white_article_wrap_detail.text_adjust_me > div.default_style_wrap.prejs_body_adjust_detail > table:nth-child(6) > tbody > tr:nth-child(${i}) > td:nth-child(3) > a`, | |
); | |
} catch {} | |
} | |
} | |
await browser.close(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment