Home > OS >  how to stop repeating same text in loops python
how to stop repeating same text in loops python

Time:09-22

from re import I
from requests import get

res = get("https://subsplease.org/api/?f=latest&tz=canada/central").json()
kek = []

for x in res:
    kek.append(x)



lnk = res[kek[0]]['downloads']
anime_name = res[kek[0]]['show']

for x in lnk:
    quality = x['res']
    links = x['magnet']

    data = f"{anime_name}:\n\n{quality}: {links}\n\n"

    print(data)

in this code how can i prevent repeating of anime name
if i add this outside of the loop only 1 link be printed

CodePudding user response:

you can separate you string, 1st half outside the loop, 2nd inside the loop:

print(f"{anime_name}:\n\n")
for x in lnk:
    quality = x['res']
    links = x['magnet']

    data = f"{quality}: {links}\n\n"

    print(data)

CodePudding user response:

Rewrote a bit, make sure you look at a 'pretty' version of the json request using pprint or something to understand where elements are and where you can loop (remembering to iterate through the dict)

from requests import get

data = get("https://subsplease.org/api/?f=latest&tz=canada/central").json()

for show, info in data.items():
    print(show, '\n')
    for download in info['downloads']:
        print(download['magnet'])
        print(download['res'])
    print('\n')

Also you won't usually be able to just copy these links to get to the download, you usually need to use a torrent website.

CodePudding user response:

You can use set to eliminate duplicate links as it only allows unique values.

lnk = list(set(res[kek[0]]['downloads']))

This will keep only unique links from your response.

  • Related