1

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.

2
  • Well, are you using Python 2 or Python 3? Does your code work? Commented Jul 30, 2022 at 6:16
  • python 3 and it works, just wanted to see if there is any efficient way Commented Jul 30, 2022 at 7:17

2 Answers 2

1

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)]
Sign up to request clarification or add additional context in comments.

Comments

1

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)

2 Comments

I don't see why this is an improvement over including the f-string inline. Lambdas have their place, but is this really a net win?
actually i dont understand what you mean ? but you are right Lambdas have their place

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.