How to Scrape Latitude & Longitude Coordinates of Location Using Python Selenium in Node.JS Express

How to Scrape Latitude & Longitude Coordinates of Location Using Python Selenium in Node.js Express

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import sys
 
def getlatlong(place):
    
    driver = webdriver.Chrome("chromedriver.exe")
    
    driver.get("https://google.com")
    
    input = driver.find_element_by_xpath(
        '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input')
    
    input.send_keys(place.strip() + " Latitude and Longitude" + Keys.ENTER)
    
    coordinates = driver.find_element_by_xpath(
        '//*[@id="rso"]/div[1]/block-component/div/div[1]/div[1]/div/div/div/div[1]/div/div/div[2]/div/div/div/div[1]').text
    
    print(coordinates)
    
 
if __name__ == "__main__":
    
    getlatlong(sys.argv[1])

const express = require('express')
 
const bodyparser = require('body-parser')
 
const {exec} = require('child_process')
 
const app = express()
 
app.use(bodyparser.urlencoded({ extended: false }))
 
app.use(bodyparser.json())
 
app.set("view engine", "ejs")
 
app.get('/', (req, res) => {
    res.render('index',{coordinates:''})
})
 
app.post('/getinfo', (req, res) => {
    exec(`python app.py ${req.body.location}`, (err, stdout, stderr) => {
        res.render('index',{coordinates:stdout})
    })
})
 
app.listen(5000)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Get Latitude and Longitude for Each Location</title>
</head>
<body>
    <form action="/getinfo" method="post">
        <input type="text" name="location" placeholder="Enter location" required id="">
        <input type="submit" value="Get Coordinates">
    </form>
    <%if(coordinates){%>
    <h1><%=coordinates%></h1>
    <%}%>
</body>
</html>

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment