checkaayush
6/20/2016 - 6:20 AM

Get Address of properties on Magicbricks.com using Reverse-Geocoding

Get Address of properties on Magicbricks.com using Reverse-Geocoding

"""
Project: Apartment Hunt

Get Address of properties on Magicbricks.com using Reverse-Geocoding

This script asks for the target property URL, reverse geocodes it 
and prints the address of the property

Need: Magicbricks map functionality doesn't let users get directions. 
Even the exact property address is not visible on the webpage.

"""

import re

import requests
from bs4 import BeautifulSoup
from geopy.geocoders import GoogleV3

# URL = ("http://www.magicbricks.com/propertyDetails/Paying-Guest-FOR-Rent-Saket-in-New-Delhi" + 
#        "&id=4d423139323339393233?from=search")

URL = raw_input("Enter Magicbricks Property URL: ")

def make_request(link):
    r = requests.get(link)
    soup = BeautifulSoup(r.text)
    return soup

def get_coords(soup):
    see_on_map_span = soup.find("span", {"class": "seeProOnMap"})
    temp_coords_list = see_on_map_span.a['onclick'].split('=')
    lat = temp_coords_list[2].split('&')[0]
    lng = temp_coords_list[3].split('&')[0]
    return lat, lng

def reverse_geocode(lat, lng):
    geolocator = GoogleV3()
    coords = ', '.join([lat, lng])
    location = geolocator.reverse(coords)
    return location

def main():
    soup = make_request(URL)
    lat, lng = get_coords(soup)
    location = reverse_geocode(lat, lng)
    print "\nAddress = ", location[0].address + "\n"

if __name__ == "__main__":
    main()