Search code examples
pythonstringurlgeturl

Python get generated url to string


(Title might change not too sure what to call it)

So I'm trying to open a URL that directs to a random page (This URL: http://anidb.net/perl-bin/animedb.pl?show=anime&do.random=1) and I want to return where that URL goes

    randomURL = urllib.urlopen("http://anidb.net/perl-bin/animedb.pl?show=anime&do.random=1")
    print(randomURL)  

That's what I (stupidly) thought would work. I imported urllib


Solution

  • In Python 3 >, urllib.urlopen was replaced by urllib.request.urlopen. Change the request line to this:

    urllib.request.urlopen('http://anidb.net/perl-bin/animedb.pl?show=anime&do.random=1')
    

    For more, you can see the docs

    But if you want to have the url, which is a bit more difficult, you can take a look at urllib.request.HTTPRedirectHandler