Skip to content

Instantly share code, notes, and snippets.

@vik-y
Created October 6, 2016 16:44
Show Gist options
  • Save vik-y/d9d1b67d4c4b94e0bebac7d92a065291 to your computer and use it in GitHub Desktop.
Save vik-y/d9d1b67d4c4b94e0bebac7d92a065291 to your computer and use it in GitHub Desktop.
A script to extract cities in which hyundai's workshops are available
'''
Copyright (c) 2016 Vikas Yadav
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''
import requests
import re
URL = 'http://www.customercare.hyundai.co.in/HyundaiGoogleMap.aspx'
VIEWSTATE = ""
EVENTVALIDATION = ""
def get_states():
global VIEWSTATE, EVENTVALIDATION
# Do the request
html = requests.get(URL).text
# Update the state variables
VIEWSTATE = get_view_state(html)
EVENTVALIDATION = get_event_validation(html)
pattern = '<option value=(.*)?>(.*)</option>'
results = re.finditer(pattern, html)
states = {}
for result in results:
code = result.groups()[0].strip('"')
if "-" not in code:
#print code, result.groups()[1]
states[code] = result.groups()[1]
return states
def parse_options(html):
pattern = '<option value=.*?>(.*)</option>'
results = re.findall(pattern, html)
return results
def get_cities(state_id):
parameters = {"__VIEWSTATE" : VIEWSTATE, "__EVENTVALIDATION" : EVENTVALIDATION, "ddlState" : state_id}
html = requests.post(URL, data=parameters).text
options = re.findall('<select .*?>(.*?)</select>', html, re.DOTALL)
#print "Printing Cities Here"
return parse_options(options[1])
def get_view_state(html):
pattern = '<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*)" />'
result = re.search(pattern, html)
return result.groups()[0]
def get_event_validation(html):
pattern = '<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="(.*)" />'
result = re.search(pattern, html)
print result.groups()[0]
return result.groups()[0]
# Main Starts here
s = get_states()
f = open("locations.csv", "w")
print "City,State"
f.write("City,State\n")
for key, value in s.items():
c = get_cities(key)
print "Cities in", value
for city in c:
print city+","+value
f.write(city+","+value+"\n")
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment