是否有圖書館或方法將電話國家代碼轉換為國家名稱。例如,將 33 轉換為法國。我似乎無法在庫(國家/地區代碼)中找到電話國家/地區代碼作為來源。
uj5u.com熱心網友回復:
您可以輕松地在線查找國家/地區撥號代碼串列并將它們讀入資料幀(例如,使用rvest)。然后,您可以使用它來查找相關的國家/地區代碼。
例如,讓我們使用搜索時得到的第一個命中international telephone codes:
url <- paste0("https://warwick.ac.uk/services/academicoffice/",
"ourservices/saro/recruitment/callingcampaign/callingcodes/")
如果我們決議這個頁面上的表格,我們會得到一個合理的結果:
library(rvest)
codes <- read_html(url) %>%
html_nodes(xpath = "//table") %>%
html_table() %>%
`[[`(1)
codes
#> # A tibble: 233 x 4
#> Country `Country Code` `International Prefix` `National Prefix`
#> <chr> <chr> <chr> <chr>
#> 1 Afghanistan 93 "00" "0"
#> 2 Albania 355 "00" "0"
#> 3 Algeria 213 "00" "7"
#> 4 American Samoa 684 "00" ""
#> 5 Andorra 376 "00" ""
#> 6 Angola 244 "00" "0"
#> 7 Anguilla 1-264 "011" "1"
#> 8 Antarctica 672 "" ""
#> 9 Antigua 1-268 "011" "1"
#> 10 Argentina 54 "00" "0"
#> # ... with 223 more rows
為了得到你想要的格式,我們可以從第二列的數字中洗掉“ ”號:
codes$`Country Code` <- sub("\\ ", "", codes$`Country Code`)
為了便于使用,讓我們撰寫一個小函式來查找我們的數字代碼:
lookup_code <- function(x) {
codes$Country[codes$`Country Code` == x]
}
現在允許:
lookup_code(33)
#> [1] "France"
lookup_code(44)
#> [1] "United Kingdom"
lookup_code(1)
#> [1] "Canada" "USA Area Codes"
如果你不想每次都抓取一個網頁,這里有一個你可以使用的帶有“國家”和“代碼”列的資料框(取自同一來源):
df <- structure(list(country = c("Afghanistan", "Albania", "Algeria",
"American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica",
"Antigua", "Argentina", "Armenia", "Aruba", "Ascension", "Australia",
"Australian External Territories", "Austria", "Azerbaijan", "Bahamas",
"Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize",
"Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia & Herzegovina",
"Botswana", "Brazil", "British Virgin Islands", "Brunei Darussalam",
"Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon",
"Canada", "Cape Verde Islands", "Cayman Islands", "Central African Republic",
"Chad", "Chatham Islands (New Zealand)", "Chile", "China (PRC)",
"Christmas Island", "Colombia", "Comoros", "Congo", "Congo(Democratic Republic of)",
"Cook Islands", "Costa Rica", "Cote d'Ivoire (Ivory Coast)",
"Croatia", "Cuba", "Cyprus", "Czech Republic", "Denmark", "Diego Garcia",
"Djibouti", "Dominica", "Dominican Republic", "East Timor", "Easter Island",
"Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea",
"Estonia", "Ethiopia", "Falkland Islands", "Faroe Islands", "Fiji Islands",
"Finland", "France", "French Antilles", "French Guiana", "French Polynesia",
"Gabonese Republic", "Gambia", "Georgia", "Germany", "Ghana",
"Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe",
"Guam", "Guatemala", "Guinea Bissau", "Guinea", "Guyana", "Haiti",
"Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia",
"Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan",
"Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea (North)",
"Korea (South)", "Kuwait", "Kyrgyz Republic", "Laos", "Latvia",
"Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania",
"Luxembourg", "Macao", "Macedonia", "Madagascar", "Malawi", "Malaysia",
"Maldives", "Mali Republic", "Malta", "Marshall Islands", "Martinique",
"Mauritania", "Mauritius", "Mayotte Island", "Mexico", "Micronesia",
"Midway Island", "Moldova", "Monaco", "Mongolia", "Montserrat",
"Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal",
"Netherlands", "Netherlands Antilles", "Nevis", "New Caledonia",
"New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island",
"Northern Marine Islands", "Norway", "Oman", "Pakistan", "Palau",
"Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines",
"Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion Island",
"Romania", "Russia", "Rwanda", "St. Helena", "St. Kitts/Nevis",
"St Lucia", "St. Pierre & Miquelon", "St. Vincent & Grenadines",
"San Marino", "Sao Tomo and Principe", "Saudi Arabia", "Senegal",
"Serbia & Montenegro", "Seychelles Republic", "Sierra Leone",
"Singapore", "Slovak Republic", "Slovenia", "Solomon Islands",
"Somalia", "South Africa", "Spain", "Sri Lanka", "Sudan", "Suriname",
"Swaziland", "Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan",
"Tanzania", "Thailand", "Togolese Republic", "Tokelau", "Tonga Islands",
"Trinidad & Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks & Caicos Islands",
"Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom",
"USA Area Codes", "US Virgin Islands", "Uruguay", "Uzbekistan",
"Vanuatu", "Vietnam", "Venezuela", "Yemen", "Zambia", "Zimbabwe"
), code = c("93", "355", "213", "684", "376", "244", "1-264",
"672", "1-268", "54", "374", "297", "247", "61", "672", "43",
"994", "1-242", "973", "880", "1-246", "375", "32", "501", "229",
"1-441*", "975", "591", "387", "267", "55", "1-284*", "673*",
"359", "226", "257", "855", "237", "1", "238", "1-345*", "236",
"235", "64", "56", "86", "61\n 08 from Australia", "57",
"269", "242", "243", "682", "506", "225", "385", "53", "357",
"420", "45", "246", "253", "1-767*", "1-809*", "670", "56", "593",
"20", "503", "240", "291", "372", "251", "500", "298", "679",
"358", "33", "596", "594", "689", "241", "220", "995", "49",
"233", "350", "30", "299", "1-473", "590", "1-671*", "502", "245",
"224", "592", "509", "504", "852", "36", "354", "91", "62", "98",
"964", "353", "972", "39", "1-876*", "81", "962", "7", "254",
"686", "850", "82", "965", "996", "856", "371", "961", "266",
"231", "218", "423", "370", "352", "853", "389", "261", "265",
"60", "960", "223", "356", "692", "596", "222", "230", "269",
"52", "691", "1-808*", "373", "377", "976", "1-664*", "212",
"258", "95", "264", "674", "977", "31", "599", "1-869*", "687",
"64", "505", "227", "234", "683", "672", "1-670*", "47", "968",
"92", "680", "507", "675", "595", "51", "63", "48", "351", "1-787 or 1",
"974", "262", "40", "7", "250", "290", "1-869*", "1-758*", "508",
"1-784*", "378", "239", "966", "221", "381", "248", "232", "65",
"421", "386", "677", "252", "27", "34", "94", "249", "597", "268",
"46", "41", "963", "886", "992", "255", "66", "228", "690", "676",
"1-868", "216", "90", "993", "1~649", "688", "256", "380", "971",
"44", "1", "1-340*", "598", "998", "678", "84", "58", "998",
"260", "263")), row.names = c(NA, -233L), class = "data.frame")
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/324561.html
