Home > OS >  How to cut multiple string from url in python
How to cut multiple string from url in python

Time:06-15

My code:

enter image description here

So the output is giving the very first line of the results list.

My question is how to cut in each "url" element in one run to get the same result? Assume for loop would do?

CodePudding user response:

You can use list comprehension.

cut = [results[i].split("/")[3].split("-")[0] for in range(len(results))]

This will give you a list of the cut result for each of the urls

CodePudding user response:

If you just want to print the values then:

list_ = ['https://ingatlan.com/xiii-ker/elado lakas/tegla-epitesu-lakas/32942874',
'https://ingatlan.com/xiii-ker/elado lakas/tegla-epitesu-lakas/32661461',
'https://ingatlan.com/xiv-ker/elado lakas/tegla-epitesu-lakas/32879321',
'https://ingatlan.com/i-ker/elado lakas/tegla-epitesu-lakas/32900903',
'https://ingatlan.com/vii-ker/elado lakas/tegla-epitesu-lakas/32772342',
'https://ingatlan.com/viii-ker/elado lakas/tegla-epitesu-lakas/32916928',
'https://ingatlan.com/vi-ker/elado lakas/tegla-epitesu-lakas/32922203',
'https://ingatlan.com/xiv-ker/elado lakas/tegla-epitesu-lakas/32947911',
'https://ingatlan.com/xix-ker/elado lakas/panel-lakas/32961930']

print(*[e.split('/')[3].split('-')[0] for e in list_], sep='\n')

Output:

xiii
xiii
xiv
i
vii
viii
vi
xiv
xix
  • Related