Automatically exported from code.google.com/p/planningalerts
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

69 rindas
3.0 KiB

  1. import urllib2
  2. import urlparse
  3. import datetime, time
  4. import BeautifulSoup
  5. from PlanningUtils import PlanningApplication, PlanningAuthorityResults
  6. date_format = "%d/%m/%Y"
  7. class CrawleyParser:
  8. comment_url_template = "http://www.crawley.gov.uk/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=561&pageCSS=&pAppNo=%(pAppNo)s&pAppDocName=%(pAppDocName)s"
  9. def __init__(self, *args):
  10. self.authority_name = "Crawley Borough Council"
  11. self.authority_short_name = "Crawley"
  12. self.base_url = "http://www.crawley.gov.uk/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=560&is_NextRow=1&accept=yes&strCSS=null&pApplicationNo=&pProposal=&pLocation=&pPostcode=&pWard=&pDateType=received&pDayFrom=%(dayFrom)s&pMonthFrom=%(monthFrom)s&pYearFrom=%(yearFrom)s&pDayTo=%(dayTo)s&pMonthTo=%(monthTo)s&pYearTo=%(yearTo)s&submit=Search"
  13. self._results = PlanningAuthorityResults(self.authority_name, self.authority_short_name)
  14. def getResultsByDayMonthYear(self, day, month, year):
  15. search_day = datetime.date(year, month, day)
  16. #- Crawley only allows searches from-to, so:
  17. next = self.base_url %{
  18. "dayFrom": day,
  19. "monthFrom": month,
  20. "yearFrom": year,
  21. "dayTo": day,
  22. "monthTo": month,
  23. "yearTo": year,
  24. }
  25. # Now get the search page
  26. response = urllib2.urlopen(next)
  27. soup = BeautifulSoup.BeautifulSoup(response.read())
  28. if soup.table: #- Empty result set has no table
  29. trs = soup.table.findAll("tr")[1:] # First one is just headers
  30. for tr in trs:
  31. tds = tr.findAll("td")
  32. application = PlanningApplication()
  33. application.council_reference = tds[0].a.contents[0].strip().replace("/", "/")
  34. application.info_url = urlparse.urljoin(self.base_url, tds[0].a['href'])
  35. info_qs = urlparse.parse_qs(urlparse.urlsplit(application.info_url)[3])
  36. comment_qs = {
  37. "pAppNo": application.council_reference,
  38. "pAppDocName": info_qs["ssDocName"][0],
  39. }
  40. application.comment_url = self.comment_url_template %comment_qs
  41. application.address = tds[1].string.strip()
  42. if tds[2].string: #- if postcode present, append it to the address too
  43. application.postcode = tds[2].string.replace(" ", " ").strip()
  44. application.address += ", " + application.postcode
  45. application.description = tds[3].string.strip()
  46. application.date_received = datetime.datetime(*(time.strptime(tds[4].string.strip(), date_format)[0:6]))
  47. self._results.addApplication(application)
  48. return self._results
  49. def getResults(self, day, month, year):
  50. return self.getResultsByDayMonthYear(int(day), int(month), int(year)).displayXML()
  51. if __name__ == '__main__':
  52. parser = CrawleyParser()
  53. print parser.getResults(12,6,2008)