Lo estoy intentando ahora desde hace más de 48 horas y busqué en Google casi toda la web. Mi problema es que cuando uso titiritero, la solicitud POST no funciona; probé muchos sitios web, pero la acción del formulario POST no funciona. ¿Puede alguien ayudarme?
Archivo test.js Uso: nodo test.js
const puppeteer = require('puppeteer'); const randomUseragent = require('random-useragent'); const USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36'; (async () => { const browser = await puppeteer.launch({ args: ['--no-sandbox'] }) // headless: true, const page = await browser.newPage() await page.setViewport({ width: 1920 + Math.floor(Math.random() * 100), height: 3000 + Math.floor(Math.random() * 100), deviceScaleFactor: 1, hasTouch: false, isLandscape: false, isMobile: false, }); const userAgent = randomUseragent.getRandom(); const UA = userAgent || USER_AGENT; await page.setUserAgent(UA); await page.setJavaScriptEnabled(true); await page.setDefaultNavigationTimeout(0); await page.setRequestInterception(true); page.on("request", interceptedRequest => { var data = { "method": "POST", "postData": "URLz=VIDEOURL" }; interceptedRequest.continue(data); }); const response = await page.goto('https://fdown.net/download.php') //const responseBody = await response.text(); await page.screenshot({ path: 'affe.png', fullPage: true }) await browser.close() })()Primero debe poder hacerlo manualmente desde un navegador, en la ventana de la consola de la pestaña de desarrollador:
// see https://stackoverflow.com/questions/6396101/pure-javascript-send-post-data-without-a-form var xhr = new XMLHttpRequest(); xhr.open("POST", "https://fdown.net/download.php", true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.onreadystatechange = function () { if (this.readyState != 4) return; if (this.status == 200) { var data = this.responseText; // we get the returned data console.log(data) } else { console.warn("POST error"); } // end of state change: it can be after some time (async) }; xhr.send("URLz=VIDEOURL"); y luego puede hacer que el titiritero lo haga en la página usando page.evaluate que ejecuta el código en la página de destino:
const postResult = await page.evaluate(() => { return new Promise((resolve, reject) => { // see https://stackoverflow.com/questions/6396101/pure-javascript-send-post-data-without-a-form var xhr = new XMLHttpRequest(); xhr.open("POST", "https://fdown.net/download.php", true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.onreadystatechange = function () { if (this.readyState != 4) return; if (this.status == 200) { var data = this.responseText; // we get the returned data console.log(data); resolve(data); } else { // error reject(this) } // end of state change: it can be after some time (async) }; xhr.send("URLz=VIDEOURL"); }); });