将这个URL的实心字符串拆分为Python列表?

2024-05-13 05:45:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个坚实的字符串的网址,我从一个工具,我正在建设回来,但我无法找出如何把它变成一个有效的列表。已经尝试了几个模块和分裂参数,但没有运气。你知道吗

http://www.nicolasotr.comhttp://www.nicolasrestaurant.com

Tags: 模块工具字符串comhttp列表参数www
2条回答
result = "http://www.nicolasotr.comhttp://www.nicolasrestaurant.com"
l = ['http'+x for x in result.split('http') if x]

打印(l)

['http://www.nicolasotr.com', 'http://www.nicolasrestaurant.com']

但是您应该编辑您的工具以返回良好的值

您可以使用regex:(?:http[s]?://)(?:(?!http[s]?://).)*使用re模块。这将查找第一个出现的“http”,并进行匹配,直到找到下一个出现的“http”。你知道吗

import re

urls = "http://www.nicolasotr.comhttp://www.nicolasrestaurant.com"
results = re.findall("(?:http[s]?://)(?:(?!http[s]?://).)*", urls)

>>> results
['http://www.nicolasotr.com', 'http://www.nicolasrestaurant.com']

但正如其他人所说,修复原来的工具以便输出分离的url会更容易。你知道吗

相关问题 更多 >