flask - how do I modify a url that I pick at random in python -
i have app show images reddit. images come http://imgur.com/cuv9oau, when need make them http://i.imgur.com/cuv9oau.jpg. add (i) @ beginning , (.jpg) @ end.
this function should need. expanded on @jh314's response , made code little less compact , checked url started http://imgur.com
code cause issues other urls, google search included. replaces first instance, causes issues.
def fiximgurlinks(url): if url.lower().startswith("http://imgur.com"): url = url.replace("http://imgur", "http://i.imgur",1) # replace first instance. if not url.endswith(".jpg"): url +=".jpg" return url u in ["http://imgur.com/cuv9oau","http://www.google.com/search?q=http://imgur"]: print fiximgurlinks(u)
gives:
>>> http://i.imgur.com/cuv9oau.jpg >>> http://www.google.com/search?q=http://imgur
Comments
Post a Comment