[英]How to turn inputted words into a list of URLs in Python?
I am a newbie.我是新手。 I am trying to generate a list of links from words inputted in Python. But it always produces only the last link although I input more than one.我正在尝试从 Python 中输入的单词生成链接列表。但它总是只生成最后一个链接,尽管我输入了多个链接。
I have tried to look for the answers but just cannot seem to find the right one.我试图寻找答案,但似乎找不到正确的答案。 Maybe I am still too inexperienced to understand and apply them to my case.也许我还太缺乏经验,无法理解并将它们应用到我的案例中。 Anyway, how can I fix this?无论如何,我该如何解决这个问题?
My code:我的代码:
search = input()
Split = search.split(' ')
print(Split)
for word in Split:
URL = 'https://samplenotspamdotcom/'+word
print(URL)
URL_List = URL.split(' ')
print(URL_List)
My output:我的 output:
['https://samplenotspamdotcom/not']
Expected output:预计 output:
['https://samplenotspamdotcom/why', 'https://samplenotspamdotcom/not']
You have two options to achieve that first option is to concatenate links with a space in between each link and then split them on space or create a blank list and in loop add them to the list.您有两种选择来实现,第一种选择是将链接与每个链接之间的空格连接起来,然后在空格上拆分它们,或者创建一个空白列表并循环将它们添加到列表中。
Option 1:选项1:
URL = ''
for word in Split:
URL += 'https://samplenotspamdotcom/'+word+' '
print(URL)
URL.strip() #to remove any extra space on the end
URL_List = URL.split(' ')
print(URL_List)
Option2选项2
URLS = []
for word in Split:
URLS.append('https://samplenotspamdotcom/'+word)
print(URLS)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.