LoginSignup
3
2

More than 5 years have passed since last update.

国コード一覧取得

Last updated at Posted at 2018-01-08

wikipedia の国コードから、scraping して、国コードを取得するjupyter notebook をつくりました。

内容をここにもはっておきます。

国名 ISO 3166-1を wikipedia から取得

コードへのリンク

#!/usr/bin/env python
# -*- encoding:utf-8 -*-
import os
import sys
import re
import codecs
import requests
import json
from bs4 import BeautifulSoup


exp_url = 'https://ja.wikipedia.org/wiki/ISO_3166-1'
req = requests.get(exp_url)
#req.encoding = 'Shift_JIS'
# parser は、html.parser をつかう(lxmlはインストールが必要なため)
soup = BeautifulSoup(req.text,'html.parser')

# table 取得
tables = soup.find_all('table')
# 最初のテーブルにリンクがはいっている
trs = tables[0].find_all('tr')
iso3166_list = []
iso3166_dict['flag'] = {}
iso3166_dict['jpn']  = {}
iso3166_dict['eng'] = {}
iso3166_dict['numeric'] = {}
iso3166_dict['alpha-2'] = {}
iso3166_dict['location'] = {}
for tr in trs:
  try:
    tds = tr.find_all('td')
    if tds[0].find('a') is not None:
      id3 = tds[3].text.lower()
      iso3166_list.append(id3)
      iso3166_dict['flag'][id3] = tds[0].find('a').attrs['href']
      iso3166_dict['jpn'][id3] = tds[0].find_all('a')[1].attrs['title']
      iso3166_dict['eng'][id3] = tds[1].text
      iso3166_dict['numeric'][id3] = tds[2].text
      iso3166_dict['alpha-2'][id3] = tds[4].text
      iso3166_dict['location'][id3] = tds[5].text
  except:
    pass
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2