Skip to content

Instantly share code, notes, and snippets.

@checkaayush
Last active June 20, 2016 06:23
Show Gist options
  • Save checkaayush/2532f640437baacfd98bb21c5d8661bc to your computer and use it in GitHub Desktop.
Save checkaayush/2532f640437baacfd98bb21c5d8661bc to your computer and use it in GitHub Desktop.
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment