mel
mel

Reputation: 2790

URL generator in python

I've got an url which got the following pattern:

http://www.xf.com/position/?number=1&From=top&To=bottom

I want to generate a crawl listing given a list of position

positionToCrawl = [top, bottom, left, center, right]

I will iterate over that list but what I want to do is create an url, like:

http://www.xf.com/position/?number=1&From=X&To=Y

And to be able to replace X and Y with the element of my list, like

URLToReplace = http://www.xf.com/position/?number=1&From=X&To=Y
URLToCrawl = replaceElementXY(URLToReplace, "left", "right")
print URLToCrawl // Display: http://www.xf.com/position/?number=1&From=left&To=right 

If you guys know a library that do something like that would be great other way, I guess I will have to implement it, but I'm sure this has already been implemented, but I didn't find it.

Upvotes: 0

Views: 367

Answers (1)

Morgan Thrapp
Morgan Thrapp

Reputation: 9986

You can just use .format() for this:

'http://www.xf.com/position/?number=1&From={0}&To={1}'.format('left', 'right')

Upvotes: 2

Related Questions