-
I am encountering a 504 Gateway Timeout error when making API calls
for a web scraping task on my deployed application. The same code
works perfectly fine in my local environment without any timeout
issues. -
Below is the code where i am scrapping and getting 504 but its
getting scrapedasync function scrapeMyntra(url) {
// Set up Chrome options
const options = new chrome.Options();
options.addArguments(‘–headless’);
options.addArguments(‘–no-sandbox’);
options.addArguments(‘–disable-dev-shm-usage’);
options.addArguments(‘–disable-http2’);
options.addArguments(‘–disable-gpu’);
options.addArguments(‘user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36’);// Initialize the WebDriver
const driver = await new Builder().forBrowser(‘chrome’).setChromeOptions(options).build();try {
await driver.get('https://proxyium.com/'); try { const consentButton = await driver.wait(until.elementLocated(By.css('.fc-button.fc-cta-consent.fc-primary-button')), 10000); await consentButton.click(); } catch (error) { console.log('Consent button not found or could not be clicked. Proceeding...'); } const searchInput = await driver.wait(until.elementLocated(By.id('unique-form-control')), 10000); await searchInput.sendKeys(url); const submitButton = await driver.findElement(By.id('unique-btn-blue')); await submitButton.click(); await driver.wait(until.elementLocated(By.css('h1.pdp-title')), 10000); const getText = async (selector, defaultValue = '') => { try { const element = await driver.findElement(By.css(selector)); return await element.getText(); } catch (error) { return defaultValue; } }; // Function to get background image URL from a CSS property const getImageUrl = async (selector, defaultValue = '') => { try { const element = await driver.findElement(By.css(selector)); const style = await element.getAttribute('style'); const urlMatch = style.match(/url(["']?([^"']+)["']?)/); return urlMatch ? urlMatch[1] : defaultValue; } catch (error) { console.error(`Error fetching image for selector "${selector}":`, error); return defaultValue; } }; // Get product details const title = await getText('h1.pdp-title', ''); const description = await getText('.pdp-product-description-content', ''); const discount_price = await getText('.pdp-price strong', ''); const originalPrice = await getText('.pdp-mrp s', ''); const original_price = originalPrice.replace(/MRPs*/, '').trim(); let percentage_off = await getText('span.pdp-discount', '0%'); percentage_off = percentage_off.replace('OFF', '').replace(/[()s]/g, '').trim(); const photo = await getImageUrl('.image-grid-image', ''); // Construct the response object const responseObject = { title, original_price: original_price ? original_price : "₹0", discount_price: discount_price ? discount_price : "₹0", percentage_off: percentage_off ? percentage_off :"0%", photo, description: description ? description : title }; return responseObject;
} finally {
await driver.quit(); // Close the browser
}
}