def init(request): key = get_value(request, 'GET', 'key') if key != PASS: return HttpResponse('Access denied. You do not have a authorization for initializing database') for code in CITY_CODE: for sggcitycode in get_sggcitycode(code): values = urllib.urlencode(dict(cityCode=code, sggCityCode=sggcitycode)) url = urlparse.urljoin(BASE_URL, PATH+values) soup = Soup(opener.open(url, timeout=10)) for cols in [tr.findAll('td') for tr in soup.find('table').findAll('tr')[1:]]: save_candidate({ 'city': CITY_CODE[code], 'area': cols[0].text, 'thumbnail': cols[1].input['src'], 'number': cols[2].text, 'party': cols[3].text, 'name': cols[4].text.split('(')[0], 'gender': cols[5].text, 'birth': cols[6].text.split('/')[0], 'address': cols[7].text, 'job': cols[8].text, 'education': cols[9].text, 'work': cols[10].text, }) return HttpResponse('Done')
'thumbnail': member.thumbnail, 'name': member.name, 'birth': member.birth.strftime("%Y-%m-%d"), 'party': member.party.name, 'area': member.area, 'committee': member.committee, 'elections': [election.number for election in member.elections.all()], 'description': member.description, 'email': member.email, 'office_phone': member.office_phone, 'homepage': member.homepage, }) if format=='json': return HttpResponse(json.dumps(D), mimetype='application/json') else: return render_to_response('xml/get_members.xml', D, mimetype='text/xml') def get_assembly_detail_codes(): values = urllib.urlencode({'start': 1, 'display': 300, 'cate_url': 'mem_search01.jsp'}) try: response = urllib2.urlopen(urljoin(ASSEMBLY_URL, CODE_PATH), values, timeout=10) except urllib2.URLError, e: return None soup = Soup(response.read()) codes = [] for li in soup.findAll('li')[6:]: codes.append(li.find('div', 'fl').a['href'].split('memCode=')[1]) return codes
def get_sggcitycode(code): values = urllib.urlencode(dict(cityCode=code, sggCityCode=0)) url = urlparse.urljoin(BASE_URL, PATH+values) soup = Soup(opener.open(url, timeout=10)) return [option['value'] for option in soup.find('select', {'id': 'sggCityCode'}).findAll('option')[1:]]