<jupyter_start><jupyter_text>Load and save html with zip codes<jupyter_code>page_url = 'http://belpost.by/branch/post/otdeleniya-sviazi/'
html_page_name = 'datas/otdeleniya-sviazi.html'
source_html = requests.get(page_url).text

with open(html_page_name, 'w') as html_file:
    html_file.write(source_html)

print('Save source page to html file - done!')
with open(html_page_name) as html_file:
    soup = BeautifulSoup(html_file, 'html.parser')

print('soup is ready:', soup.title.text)<jupyter_output>soup is ready: Адреса и режим работы отделений почтовой связи (ОПС) и пунктов почтовой связи производства “Минская почта” - Белпочта<jupyter_text>Zip Code Parser<jupyter_code>table_body = soup.find_all('table', attrs={'border': 1})
len(table_body)
all_table_rows = table_body[1].find_all('tr')
def zip_code_parser():
    error = 0
    zip_codes_minsk_list = []
    for i, row in enumerate(all_table_rows, 0):
        cols_all = row.find_all('td')
        try:
            address_full = cols_all[1].find_all('div')

            if address_full and len(address_full) > 1:
                street = address_full[0].string
                if not street:
                    street = address_full[0].text
                zip_code = address_full[1].string
                if not zip_code:
                    zip_code = address_full[1].text

            elif not address_full or len(address_full) == 1:
                some_content = cols_all[1].contents
                if len(some_content) == 1:
                    some_content = cols_all[1].find('div').contents

                address = [(q.string).replace('\xa0', ' ').strip() 
                           for q in some_content if q.string]

                street = address[0]
                zip_code = address[1]
            else:
                continue

            street = street.replace('\xa0', ' ').strip()
            zip_code = zip_code[:6]

            zip_codes_minsk_list.append([street, int(zip_code), 0.0, 0.0])

        except Exception as ex:
            error += 1
            print('Exception in address_full: %s' % ex, i)

    print('errors:', error)
    print('zip_codes_minsk_list:', len(zip_codes_minsk_list))
    return zip_codes_minsk_list
zip_codes_minsk_list = zip_code_parser()<jupyter_output>Exception in address_full: 'NoneType' object has no attribute 'contents' 0
Exception in address_full: list index out of range 116
errors: 2
zip_codes_minsk_list: 125<jupyter_text>Create Pandas dataframe<jupyter_code>pd_columns = ['address', 'zip-code', 'lat', 'lng']
df_minsk = pd.DataFrame(data=zip_codes_minsk_list, columns=pd_columns)
zip_codes = df_minsk['zip-code']
df_minsk[zip_codes.isin(zip_codes[zip_codes.duplicated()])]<jupyter_output><empty_output><jupyter_text>delete repeated zip codes<jupyter_code>df_minsk.sort_values('zip-code', inplace=True)
df_minsk.drop_duplicates(subset='zip-code', inplace=True)
df_minsk.reset_index(inplace=True)
df_minsk.head()
df_minsk.drop(columns=['index'], inplace=True)
print(len(df_minsk['zip-code'].unique()))
print(df_minsk.shape)
# df_minsk.to_csv('datas/zip_codes_minsk_list.csv')<jupyter_output><empty_output><jupyter_text>Find coordinates for all zip codes with Google Geo API<jupyter_code>def find_coords():
    for i in range(df_minsk.shape[0]):
        try:
            address = df_minsk.iloc[i, 0] + ', Minsk'

            google_geo = GoogleV3(api_key=GOOGLE_GEO_API_KEY)
            coords = google_geo.geocode(address)

            lat = float(coords.latitude)
            lng = float(coords.longitude)

            df_minsk.iloc[i, 2:4] = lat, lng
            #  print(i, lat, lng)
        except Exception as ex:
            print('Exception in google_location: %s' % ex)
# find_coords()
df_minsk.isnull().sum()
0.0 in df_minsk.values
# df_minsk.to_csv('datas/zip_codes_minsk_list.csv')
df_minsk = pd.read_csv('datas/zip_codes_minsk_list.csv')
df_minsk.drop(columns=['Unnamed: 0'], inplace=True)
df_minsk<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11930531084822552, 'fraction_numerical': 0.010067958721369242, 'mean_word_length': 3.3008658008658007, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '394389', 'license': 'MIT', 'n_tokens_mistral': 1470, 'n_tokens_neox': 1428, 'n_words': 256, 'path': 'blr/find_post_offices.ipynb', 'repo_name': 'Valentin-Golyonko/Coursera_Capstone', 'size': '3973'}