-
Notifications
You must be signed in to change notification settings - Fork 0
/
ioc_flags.py
28 lines (22 loc) · 873 Bytes
/
ioc_flags.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# -*- coding: utf-8 -*-
import csv
import requests
from bs4 import BeautifulSoup
csv_file = open("ioc_flags.csv", "w", newline='')
csv_writer = csv.DictWriter(csv_file, fieldnames=['code', 'url'])
def get_country_flags():
response = requests.get("http://en.wikipedia.org/wiki/List_of_IOC_country_codes")
soup = BeautifulSoup(response.text)
wikitables = soup.find_all(class_="wikitable")
for wikitable in wikitables:
for tr in wikitable.find_all("tr"):
td = tr.find_all("td")
if td:
country_code = td[0].text
img = td[2].find("img")
if img:
url = img.get("src").replace("22px", "200px")
csv_writer.writerow({'code': country_code, 'url': url})
if country_code == "ZIM":
return
get_country_flags()