Automatically exported from code.google.com/p/planningalerts
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

89 righe
3.7 KiB

  1. import urllib2
  2. import urllib
  3. import datetime
  4. import re
  5. from BeautifulSoup import BeautifulSoup
  6. from PlanningUtils import PlanningApplication, \
  7. PlanningAuthorityResults, \
  8. getPostcodeFromText
  9. date_format = "%d/%m/%Y"
  10. class WestDorsetParser:
  11. def __init__(self, *args):
  12. self.authority_name = "West Dorset District Council"
  13. self.authority_short_name = "West Dorset"
  14. self.base_url = "http://webapps.westdorset-dc.gov.uk/planningapplications/pages/applicationsearch.aspx"
  15. self.info_url = "http://webapps.westdorset-dc.gov.uk/planningapplications/pages/ApplicationDetails.aspx?Application=%s&Authority=West+Dorset+District+Council+"
  16. self._results = PlanningAuthorityResults(self.authority_name, self.authority_short_name)
  17. def getResultsByDayMonthYear(self, day, month, year):
  18. search_date = datetime.date(year, month, day)
  19. get_response = urllib2.urlopen(self.base_url)
  20. get_soup = BeautifulSoup(get_response.read())
  21. post_data = (
  22. ("__VIEWSTATE", get_soup.find("input", id="__VIEWSTATE")["value"]),
  23. # ("QuickSearchApplicationNumber$TextBox_ApplicationNumber", ""),
  24. # ("QuickSearchThisWeek$DropDownList_PastWeek", ""),
  25. # ("DetailedSearch$TextBox_PropertyNameNumber", ""),
  26. # ("DetailedSearch$Textbox_StreetName", ""),
  27. # ("DetailedSearch$Textbox_TownVillage", ""),
  28. # ("DetailedSearch$Textbox_Postcode", ""),
  29. # ("DetailedSearch$Textbox_Parish", ""),
  30. # ("DetailedSearch$Textbox_ApplicantSurname", ""),
  31. # ("DetailedSearch$TextBox_AgentName", ""),
  32. ("DetailedSearch$TextBox_DateRaisedFrom", search_date.strftime(date_format)),
  33. ("DetailedSearch$TextBox_DateRaisedTo", search_date.strftime(date_format)),
  34. # ("DetailedSearch$TextBox_DecisionFrom", "dd%2Fmm%2Fyyyy"),
  35. # ("DetailedSearch$TextBox_DecisionTo", "dd%2Fmm%2Fyyyy"),
  36. ("DetailedSearch$Button_DetailedSearch", "Search"),
  37. ("__EVENTVALIDATION", get_soup.find("input", id="__EVENTVALIDATION")["value"]),
  38. )
  39. # The response to the GET is a redirect. We'll need to post to the new url.
  40. post_response = urllib2.urlopen(get_response.url, urllib.urlencode(post_data))
  41. post_soup = BeautifulSoup(post_response.read())
  42. if not post_soup.find(text = re.compile("No matching record")):
  43. # The first row contains headers.
  44. trs = post_soup.find("table", {"class": "searchresults"}).findAll("tr")[1:]
  45. for tr in trs:
  46. application = PlanningApplication()
  47. # We can fill the date received in straight away from the date we searched for.
  48. application.date_received = search_date
  49. tds = tr.findAll("td")
  50. application.council_reference = tds[0].font.string.strip()
  51. application.address = tds[2].font.string.strip()
  52. application.postcode = getPostcodeFromText(application.address)
  53. application.description = tds[3].font.string.strip()
  54. # Set the info url and the comment url to be the same - can't get to the comment
  55. # one directly without javascript.
  56. application.info_url = self.info_url %(application.council_reference)
  57. application.comment_url = application.info_url
  58. self._results.addApplication(application)
  59. return self._results
  60. def getResults(self, day, month, year):
  61. return self.getResultsByDayMonthYear(int(day), int(month), int(year)).displayXML()
  62. if __name__ == '__main__':
  63. parser = WestDorsetParser()
  64. print parser.getResults(1,10,2008)