Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save harsh317/8ab252da56526577119ff8e20af8b602 to your computer and use it in GitHub Desktop.
Save harsh317/8ab252da56526577119ff8e20af8b602 to your computer and use it in GitHub Desktop.
A simple file to get distance result from google using beautifulsoup
import requests
import re
from bs4 import BeautifulSoup
headers = {
"User-Agent":'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'}
st = input("enter from:")
sto = input("enter to:")
base = 'https://www.google.com/search?safe=active&rlz=1C1YQLS_enIN897IN897&biw=1366&bih=657&sxsrf=ALeKk02Y8GEcD4XWh64yCvecTCetld-HZQ%3A1608364202503&ei=qrDdX5WVHtSprtoPmu6HiA0&q=distance+between+'
url = (base+st+'+and+'+sto)
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
price = soup.find('span', attrs={'class':'UdvAnf'}).get_text()
temp = re.sub('^.*\((.*?)\)[^\(]*$', '\g<1>', price)
temp = temp.replace(',', '')
if 'km' in temp:
u = 'km'
elif 'mi' in temp:
u = 'Mi'
temp1 = re.findall("\d+\.\d+", temp)
numprice = float(''.join(temp1))
print(numprice,u)
@harsh317
Copy link
Author

use this kind of similar logic in django to make a Geo Django APP

@harsh317
Copy link
Author

Distance matrix Api can also be used

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment