Search code examples
pythonmodulepython-webbrowser

Open a specific page on a website


Using the webbrowser module, I want to open a specific page on last.fm. It picks a line from a text file then prints it. I want it to add that line at the end of:

webbrowser.open('http://www.last.fm/music/')

So for example, the random.choice picks example artist. I want example artist to be added at the end of the url correctly. Any help is appreciated.


Solution

  • Use the urlparse.urljoin function to build up the full destination URL:

    import urlparse
    import webbrowser
    
    artist_name = 'virt' 
    url = urlparse.urljoin('http://www.last.fm/music/', artist_name)
    
    # Will open http://www.last.fm/music/virt in your browser.
    webbrowser.open(url)