Home > Mobile >  For loop append string to empty list, by incrementing value in string
For loop append string to empty list, by incrementing value in string

Time:07-30

I sorta mix together codes i copy pasted everywhere to achieve what i want but i felt it is rather weird and probably not so efficient/not pythonic. So what i tried to get is substitute the last item in the url after "/" by incremental value of 1 and then append to the list "url_pages".

["https://www.jobstreet.com.my/en/job-search/{pos}-jobs-in-{loc}/1", "https://www.jobstreet.com.my/en/job-search/{pos}-jobs-in-{loc}/2", .....]

i read that %s is for python 2.x while f-string is the modern way so is there any way to achieve this not using %s.

selectpage = soup.find("select", id="pagination")
maxpage = int(selectpage.find_all("option")[-1].get("value"))

url_page = []
i = 1
pos = input(str("input pos: ")).replace(" ", "-")
loc = input(str("input loc :")).replace(" ", "-")
finalurl = f"https://www.jobstreet.com.my/en/job-search/{pos}-jobs-in-{loc}/%s"

for i in range(1,maxpage 1):
    if i <= maxpage:
        url_page.append(finalurl % i)
    else:
        print("exceeded max page")
    i  = 1

print(url_page)

Thanks.

CodePudding user response:

You can replace that loop by list comprehension -- there is no need to test that i exceeds the limit as this will never happen. Also incrementing i in the loop is no needed as that is already taken care of by range.

The format string can get {{}} (escaped braces) so you can call .format(i) later to inject the value of i:

finalurl = f"https://www.jobstreet.com.my/en/job-search/{pos}-jobs-in-{loc}/{{}}"

url_page = [finalurl.format(i) for i in range(1, maxpage 1)]

CodePudding user response:

you can use lambda function to put parameter in string line

simply use it like that:

w =lambda n: f"you are welocme  {n} :)"
w("Mohamed")

in your code:

selectpage = soup.find("select", id="pagination")
maxpage = int(selectpage.find_all("option")[-1].get("value"))

url_page = []
i = 1
pos = input(str("input pos: ")).replace(" ", "-")
loc = input(str("input loc :")).replace(" ", "-")
finalurl = lambda page: f"https://www.jobstreet.com.my/en/job-search/{pos}-jobs-in-{loc}/{page}"

for i in range(1,maxpage 1):
    if i <= maxpage:
        url_page.append(finalurl(i))
    else:
        print("exceeded max page")
    i  = 1

print(url_page)
  • Related