简体   繁体   中英

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

I have a variable called all_urls - it contains a list of URLs, for example:

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

I want to print each URL using a for loop.

for url in all_urls:
        print(url)

but what I end up getting is:

h
t
t
p
s
(and so on)

Does anyone know how to fix it so the program returns each URL? Thanks.

You can try with this:

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

What I'm thinking is that you have a string rather than a list, so when you cycle over all elements of your variable, it just prints the single character.

In this way, split('\n') will allow to generate a list by splitting on the new line, each of which element will be the single url.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM