如何使用regex从url中提取特定的

llmtgqce  于 2023-05-19  发布在  其他
关注(0)|答案(1)|浏览(158)

我需要提取具体的数字和文字的网址

import re
text = """https://bingo.render.com/vegenpropertymanagementllc.messio.com/2231/workOrders/9552?idp_type"""
work_order_id = re.search(r'workOrders/(\d+)/',text)
print(work_order_id)
company = re.search(r'.com/(.*)/',text)
print(company.group(1))

所需解决方案:

company = vegenpropertymanagementllc.messio.com
work_order_id = 9552
s6fujrry

s6fujrry1#

您可以使用urllib.parse轻松地隔离这两个必需的值,如下所示:

from urllib import parse

URL = 'https://bingo.render.com/vegenpropertymanagementllc.messio.com/2231/workOrders/9552?idp_type'

_, company, *_, work_order_id = parse.urlsplit(URL).path.split('/')

print(f'{company=}')
print(f'{work_order_id=}')

输出:

company='vegenpropertymanagementllc.messio.com'
work_order_id='9552'

相关问题