簡體   English   中英

Python - 如何在 for 循環中打印整個 url 而不僅僅是字符

[英]Python - How to print a whole url in a for loop not just characters

我有一個名為all_urls的變量 - 它包含一個 URL 列表,例如:

https://website.com/image1
https://website.com/image2
https://website.com/image3

我想使用 for 循環打印每個 URL。

for url in all_urls:
        print(url)

但我最終得到的是:

h
t
t
p
s
(and so on)

有誰知道如何修復它以便程序返回每個 URL? 謝謝。

你可以試試這個:

for url in all_urls.split('\n'):
    print(url)

我在想的是你有一個字符串而不是一個列表,所以當你循環遍歷變量的所有元素時,它只會打印單個字符。

這樣, split('\n')將允許通過在新行上拆分來生成一個列表,其中每個元素都是單個 url。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM