Skip to content

Instantly share code, notes, and snippets.

@phrz
Created May 14, 2018 22:58
Show Gist options
  • Select an option

  • Save phrz/f262af9c6ee348cca125c4bfd232e7b6 to your computer and use it in GitHub Desktop.

Select an option

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
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