Created
November 15, 2024 17:40
-
-
Save ahmadrosid/cb2cf9d7d7c467908eef8addbb1d9087 to your computer and use it in GitHub Desktop.
Get google trend using selenium scripts.
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
const {Builder, By, Key, until} = require('selenium-webdriver'); | |
const chrome = require('selenium-webdriver/chrome'); | |
const options = new chrome.Options(); | |
// Add headless mode configurations | |
options.addArguments('--headless'); // Run in headless mode | |
options.addArguments('--disable-gpu'); // Disable GPU hardware acceleration | |
options.addArguments('--no-sandbox'); // Bypass OS security model | |
options.addArguments('--disable-dev-shm-usage'); // Overcome limited resource problems | |
async function main() { | |
// Initialize driver with headless options | |
let driver = await new Builder() | |
.forBrowser('chrome') | |
.setChromeOptions(options) | |
.build(); | |
try { | |
// Rest of your code remains the same | |
await driver.get('https://trends.google.com/trending?geo=ID&hl=en&hours=24'); | |
await driver.wait(until.elementLocated(By.xpath("//table/tbody[@jsname='cC57zf']/tr")), 10000); | |
const trendingRows = await driver.findElements(By.xpath("//table/tbody[@jsname='cC57zf']/tr")); | |
for(let i = 0; i < 25 && i < trendingRows.length; i++) { | |
const row = trendingRows[i]; | |
const title = await row.findElement(By.xpath(".//div[contains(@class, 'mZ3RIc')]")).getText(); | |
const searchVolume = await row.findElement(By.xpath(".//div[contains(@class, 'p6GDQc')]/div[contains(@class, 'lqv0Cb')]")).getText(); | |
const time = await row.findElement(By.xpath(".//div[contains(@class, 'vdw3Ld')]")).getText(); | |
console.log(`${i + 1}. Keyword: ${title}`); | |
console.log(` Search Volume: ${searchVolume}`); | |
console.log(` Posted: ${time}\n`); | |
} | |
} catch (error) { | |
console.error('An error occurred:', error); | |
} finally { | |
await driver.quit(); | |
} | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment