Search code examples
pythonpython-webbrowser

Trying to open some pages with webbrowser.open


Here is my code:

import time
import webbrowser

for k in range(3):
    webbrowser.open("[Censured]index=k")
    time.sleep(5)
    print("Téléchargement du fichier numéro", k)

So what I want to do is to open the webpage [Censured]index=1. Then, [Censured]index=2, Censured]index=3 etc... But I don't understand how to change the variable k in this code.

It opens URL [Censured]index=k and if I do change line 4 into webbrowser.open("[Censured]index=",k) it is the same problem.

How can I fix this?


Solution

  • webbrowser.open("[Censured]index="+str(k))
    

    will get the job done!! if all assumed details are correct like the link,etc.

    The reason why your approach won't work is that in the first case when you are passing"[Censured]index=k" as an argument the it will be treated as a whole string and the value of k won't change anyway , for example:

    for i in range(5):
        print "The number is i"
    

    The output will be:

    >>> The number is i
    >>> The number is i
    >>> The number is i
    >>> The number is i
    >>> The number is i
    

    And in the second case when you tried "[Censured]index=",k, Then the comma operator implicitly places a whitespace while concatenating the two results, and that would not generate a valid hyperlink. for example:

    for i in range(5):
            print "ContinuousSequence",i
    
    Output:
    >>> ContinuousSequence 0    #notice the extra space between them.
    >>> ContinuousSequence 1
    >>> ContinuousSequence 2
    >>> ContinuousSequence 3
    >>> ContinuousSequence 4