Python---爬蟲---清洗---phonenumbers(電話號碼解析)

安裝:pip3 install phonenumbers

 1.把看似不一樣的號碼規範到同一個號碼

>>> import phonenumbers
>>> x = phonenumbers.parse("+442083661177", None)
>>> y = phonenumbers.parse("020 8366 1177", "GB")
>>> x == y
True

2.檢查是否是一個“可能”的號碼

>>> z = phonenumbers.parse("+120012301", None)
>>> phonenumbers.is_possible_number(z)  # too few digits for USA
False
>>> phonenumbers.is_valid_number(z)
False

3.將電話號碼格式化到一個標準的格式

>>> phonenumbers.format_number(x, phonenumbers.PhoneNumberFormat.NATIONAL)
u'020 8366 1177'
>>> phonenumbers.format_number(x, phonenumbers.PhoneNumberFormat.INTERNATIONAL)
u'+44 20 8366 1177'
>>> phonenumbers.format_number(x, phonenumbers.PhoneNumberFormat.E164)
u'+442083661177'

4. 從一大塊文本中提取電話號碼

>>> text = "Call me at 510-748-8230 if it's before 9:30, or on 703-4800500 after 10am."
>>> for match in phonenumbers.PhoneNumberMatcher(text, "US"):
...     print match
...
PhoneNumberMatch [11,23) 510-748-8230
PhoneNumberMatch [51,62) 703-4800500
>>> for match in phonenumbers.PhoneNumberMatcher(text, "US"):
...     print phonenumbers.format_number(match.number, phonenumbers.PhoneNumberFormat.E164)
...
+15107488230
+17034800500

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章