Created
October 17, 2017 11:49
-
-
Save onesup/59f192f08a356d4ed7130cf41764f5e6 to your computer and use it in GitHub Desktop.
한국 공인중개사 협회 에서 중개사 목록 받아서 엑셀 파일로 만들기 스크립트
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bs4 import BeautifulSoup | |
from urllib.parse import urlparse | |
import xlsxwriter | |
import re | |
import requests | |
import urllib | |
import unicodedata | |
def page_url(locations, page): | |
uri = "http://www.kar.or.kr/pinfo/brokersearch.asp" | |
addrs = [] | |
# asp 로 만들어진 페이지라 질의어를 cp949 형태로 만들어서 요청해야 함. | |
for location in locations: | |
addrs.append(urllib.parse.quote(location, encoding='cp949')) | |
query = "?addr1=" + addrs[0] + "&addr2=" + addrs[1] + "&addr3=" + addrs[2] | |
query = query + "&pg=" + str(page) | |
result = uri + query | |
return result | |
def targeted_page(location, p): | |
uri = page_url(location, p) | |
s = requests.Session() | |
html = s.get(uri).text | |
return html | |
def last_page(html): | |
soup = BeautifulSoup(html, 'html.parser') | |
span = soup.find_all('span', class_="page")[-1] | |
# 동네에 업소가 하나도 없을 때. page ui 가 나오지 않음. | |
if(len(span.find_all('a')) == 0): | |
return 0 | |
uri = span.find_all('a')[-1]['href'] | |
# 동네에 없소가 10 페이지 이하로 있을 때. 끝으로 버튼에 url 이 없음. | |
fallback_uri = span.find_all('a')[-3]['href'] | |
try: | |
query = urllib.parse.parse_qs(uri)['/pinfo/brokersearch.asp?pg'][0] | |
except: | |
query = urllib.parse.parse_qs(fallback_uri)['/pinfo/brokersearch.asp?pg'][0] | |
return int(query) | |
def parsed_all_pages(location): | |
first_page = targeted_page(location, 1) | |
last = last_page(first_page) | |
if(last == 0): | |
return [] | |
last += 1 | |
page_elements = [] | |
for p in range(1, last): | |
page = targeted_page(location, p) | |
print(str(p) + "/" + str(last - 1) + location[-1]) | |
elements = parsed_page(page) | |
page_elements += elements | |
return page_elements | |
def parsed_page(html): | |
html = html.replace('</a>', '') | |
soup = BeautifulSoup(html, 'html.parser') | |
elements = [] | |
rows = soup.find_all("tr") | |
for row in rows: | |
columns = row.find_all("td") | |
if (len(columns) == 4): | |
data = [] | |
for column in columns: | |
# 윈도 asp 로 운영되는 사이트라 파싱된 글자 끝에 이상한게 묻는거 노멀라이징 | |
cleaned_column = unicodedata.normalize("NFKD", column.text.strip()) | |
data.append(cleaned_column) | |
elements.append(data) | |
return elements | |
def export_excel(elements, locations): | |
worksheet = workbook.add_worksheet(locations[-1]) | |
row = 0 | |
col = 0 | |
for company, name, address, phone in elements: | |
# 윈도 엑셀에서 한글이 풀어져서 나오는 현상 때문에 노멀라이징 | |
worksheet.write(row, col, unicodedata.normalize('NFC', company)) | |
worksheet.write(row, col + 1, unicodedata.normalize('NFC', name)) | |
worksheet.write(row, col + 2, unicodedata.normalize('NFC', address)) | |
worksheet.write(row, col + 3, unicodedata.normalize('NFC', phone)) | |
row += 1 | |
if __name__ == "__main__": | |
target_locations = [] | |
target_locations.append(['경기도', '안양시 동안구', '관양동']) | |
target_locations.append(['경기도', '안양시 동안구', '비산동']) | |
target_locations.append(['경기도', '안양시 동안구', '평촌동']) | |
target_locations.append(['경기도', '안양시 동안구', '호계동']) | |
target_locations.append(['경기도', '안양시 만안구', '박달동']) | |
target_locations.append(['경기도', '안양시 만안구', '석수동']) | |
target_locations.append(['경기도', '안양시 만안구', '안양동']) | |
target_locations.append(['경기도', '안산시 단원구', '고잔동']) | |
target_locations.append(['경기도', '안산시 단원구', '대부남동']) | |
target_locations.append(['경기도', '안산시 단원구', '대부동동']) | |
target_locations.append(['경기도', '안산시 단원구', '대부북동']) | |
target_locations.append(['경기도', '안산시 단원구', '목내동']) | |
target_locations.append(['경기도', '안산시 단원구', '선감동']) | |
target_locations.append(['경기도', '안산시 단원구', '선부동']) | |
target_locations.append(['경기도', '안산시 단원구', '성곡동']) | |
target_locations.append(['경기도', '안산시 단원구', '신길동']) | |
target_locations.append(['경기도', '안산시 단원구', '와동']) | |
target_locations.append(['경기도', '안산시 단원구', '원곡동']) | |
target_locations.append(['경기도', '안산시 단원구', '원시동']) | |
target_locations.append(['경기도', '안산시 단원구', '초지동']) | |
target_locations.append(['경기도', '안산시 단원구', '풍도동']) | |
target_locations.append(['경기도', '안산시 단원구', '화정동']) | |
target_locations.append(['경기도', '안산시 상록구', '긴건동']) | |
target_locations.append(['경기도', '안산시 상록구', '본오동']) | |
target_locations.append(['경기도', '안산시 상록구', '부곡동']) | |
target_locations.append(['경기도', '안산시 상록구', '사동']) | |
target_locations.append(['경기도', '안산시 상록구', '사사동']) | |
target_locations.append(['경기도', '안산시 상록구', '성포동']) | |
target_locations.append(['경기도', '안산시 상록구', '수암동']) | |
target_locations.append(['경기도', '안산시 상록구', '양상동']) | |
target_locations.append(['경기도', '안산시 상록구', '월피동']) | |
target_locations.append(['경기도', '안산시 상록구', '이동']) | |
target_locations.append(['경기도', '안산시 상록구', '일동']) | |
target_locations.append(['경기도', '안산시 상록구', '장상동']) | |
target_locations.append(['경기도', '안산시 상록구', '장하동']) | |
target_locations.append(['경기도', '안산시 상록구', '팔곡이동']) | |
target_locations.append(['경기도', '안산시 상록구', '팔곡일동']) | |
workbook = xlsxwriter.Workbook('members.xlsx') | |
for target_location in target_locations: | |
print(page_url(target_location, 1)) | |
all_elements = parsed_all_pages(target_location) | |
export_excel(all_elements, target_location) | |
workbook.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment