3

I would like to programatically generate a URL made by these parts

Fixed part

https://booking.snav.it/#/booking/rates/

Outbound route number - changes

1040

Outbound date - changes

19-02-2019

Inbound route number - changes

1042

Inbound date - changes

20-02-2019

Parameters:

"adults": "1"
"childs":"0"
"infants":"0"
"res": "0"
"vehicle":"0"

Output

https://booking.snav.it/#/booking/rates/1040/19-02-2019/1042/19-02-2019?adults=1&childs=0&infants=0&res=0&vehicle=0

I know how to pass parameters with urllib.parse.urlencode

params = urllib.parse.urlencode({
   "adults": "1"
    "childs":"0"
    "infants":"0"
    "res": "0"
    "vehicle":"0"
})

url = "https://booking.snav.it/#/booking/rates/"
res = requests.get(url, params=params)

but don't know how to build dynamically the first part after the fixed URL 1040/19-02-2019/1042/19-02-2019

2
  • You should dig into Python string formatting. It is one of the basic concepts of the language and you'll benefit a bunch. Commented Feb 21, 2019 at 12:13
  • Thank you - surely going to do it. Commented Feb 21, 2019 at 12:27

1 Answer 1

4

A URL is really just a string, any of the usual string manipulation techniques would do here. Your component parts don't have any characters in them that would require URL-encoding here either, making the whole process simpler.

If you do have component parts that use characters that are not in the list of unreserved characters, then use the urllib.parse.quote() function to convert those characters to URL-safe components.

You could use str.join() with / to join string parts:

outbound_route = '1040'
outbound_date = '19-02-2019'
inbound_route = '1042'
inbound_date = '20-02-2019'

url = "https://booking.snav.it/#/booking/rates"  # no trailing /
final_url = '/'.join([url, outbound_route, outbound_date, inbound_route, inbound_date])

or you could use a formatted string literal:

url = "https://booking.snav.it/#/booking/rates/"
final_url = f'{url}{outbound_route}/{outbound_date}/{inbound_route}/{inbound_date}'

This approach has the advantage that the components don't have to be strings; if outbound_route and inbound_route are integers, you don't have to explicitly convert them to strings first.

Or, since URL paths work a lot like POSIX filesystem paths, you could use the pathlib.PosixPurePath() class to contruct the path:

from pathlib import PosixPurePath

path = PosixPurePath('/booking/rates') / outbound_route / outbound_date / inbound_route / inbound_date
final_url = f"https://booking.snav.it/#{path}"

In all cases, you end up with a final URL to use in requests:

res = requests.get(final_url, params=params)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you - great comprehensive answer.

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.