Last active
December 19, 2020 08:42
-
-
Save harsh317/8ab252da56526577119ff8e20af8b602 to your computer and use it in GitHub Desktop.
A simple file to get distance result from google using beautifulsoup
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
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) |
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
use this kind of similar logic in django to make a Geo Django APP