首页 > 解决方案 > 在输入文本中查找所有网站地址(Python)

问题描述

我需要在输入文本中找到所有网站地址,并按照它们在文本中出现的顺序打印所有地址,每个地址都换行。"https://" "http://" "www."

我在字符串中使用了拆分,但我不能返回以这个“www”开头的那个。有人可以向我解释我该如何解决这个问题吗?

样本输入 1:

WWW.GOOGLE.COM 使用 100% 的可再生能源,并且www.ecosia.com每 45 次搜索就种一棵树!

样本输出 1:

万维网.GOOGLE.COM

www.ecosia.com

text = input()
text = text.lower()
words = text.split(" ")
for word in words:

标签: python-3.x

解决方案


我要做的是捕捉“www”couse',我们知道每个网址都以它开头,并以空格键结尾,所以将所有内容放入数组中然后打印它,但是python在它的库中有很多字符串函数但是我不知道很多。

str = " www.GOOGLE.COM uses 100-percent renewable energy sources and www.ecosia.com plants a tree for every 45 searches! "
str.lower()
tmp = ""
all_url = []
k=0
for i in range(len(str)-3):
    if(str[i]+str[i+1]+str[i+2] == "www"):
        k=i+4
        while(str[k] != " "):
            tmp=tmp+str[k]
            k+=1
        all_url.append(tmp)
        tmp = ""
        i=k
for url in all_url:
    print("www." + url )

推荐阅读