Created
May 14, 2018 22:58
-
-
Save phrz/f262af9c6ee348cca125c4bfd232e7b6 to your computer and use it in GitHub Desktop.
Scrape a list of all international telephone country codes (like +1) from the Wikipedia list of country codes
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
| import requests | |
| from bs4 import BeautifulSoup | |
| import re | |
| page = requests.get('https://en.wikipedia.org/wiki/List_of_country_calling_codes') | |
| soup = BeautifulSoup(page.text, 'html.parser') | |
| text = str(soup.find('table', class_='wikitable')) | |
| matches = re.findall(r'\+\d+', text) | |
| matches = set(matches) | |
| matches = filter(lambda n: len(n)==4, matches) | |
| code_strings = map(lambda c: f'"{c[1:]}"', matches) | |
| print(','.join(code_strings)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment