首页 > 解决方案 > 从 URL 中提取

问题描述

如何从 url 中提取 da_dk 部分?我正在尝试从 url 中找到国家代码和语言代码。

import re
url = https://www.url.com/content/test/abcd/da_dk/1234.html
#cc_lc = re.search(?, url)
cc ,lc = cc_lc.split(‘_’)
print(cc,lc)

标签: python

解决方案


你可以做类似的事情

import re
url = "https://www.url.com/content/test/abcd/da_dk/1234.html"
url_list = url.split('/')
for el in url_list:
    if "_" in el:
        codes = el.split("_")
        if (len(codes) == 2):
            #use regex to check coade[0] and code[1] are valid cc and lc or not

推荐阅读