简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM