如何在python中一个接一个地打开多个网页?

时间:2017-05-30 16:34:22

标签: python-2.7 loops web

让我们考虑以下例子:

import webbrowser
Cn=('Acharya Girish Chandra Bose College','AJC Bose College','Ananda Mohan College','Asutosh College','Bangabasi College','Barrackpore Rastraguru Surendranath College','Basanti Devi College')

Cnw={'Acharya Girish Chandra Bose College':'http://www.agcbosecollege.org/','AJC Bose College':'http://www.ajcbosecollege.org/','Ananda Mohan College':'http://anandamohancollege.ac.in/','Asutosh College':'http://www.asutoshcollege.in/','Bangabasi College':'http://bangabasi.org/home.php','Barrackpore Rastraguru Surendranath College':'http://www.brsnc.org/','Basanti Devi College':'http://www.basantidevicollege.edu.in/'}

yy=['Y','Yes','YES','y']
nn=['N','NO','no','No','n']
i=0
while i<=range(len(Cn)):
    print 'College Name : ',Cn[i]
    a=raw_input('Do u want to visit the website ? (Y/N) :')
    if a in yy:
        webbrowser.open(Cnw[Cn[i]])
    elif a in nn:
        break
    i=i+1

此程序运行正常,但我想在打开网页后打开下一个网站。该程序只打开一个网站,然后打开另一个网站,但每次都会跳过一个网站。这里:
Image link here

从列表中打开一个网站后,i = i + 1会增加i,然后再次要求我打开下一个网站。请建议这样做

1 个答案:

答案 0 :(得分:0)

我不确定您使用range()的原因,只需使用len(Cn)即可,但是您应该将其更改为i<len(Cn),这样您就不会查看索引在while循环的最后一次迭代中。

此外,当您在break语句中elif时,您将结束循环。我不确定这是否是你想要做的,如果你宁愿继续解析列表只需删除elif语句。您也不需要nn无变化列表。

import webbrowser
Cn=('Acharya Girish Chandra Bose College','AJC Bose College','Ananda Mohan College','Asutosh College','Bangabasi College','Barrackpore Rastraguru Surendranath College','Basanti Devi College')

Cnw={'Acharya Girish Chandra Bose College':'http://www.agcbosecollege.org/','AJC Bose College':'http://www.ajcbosecollege.org/','Ananda Mohan College':'http://anandamohancollege.ac.in/','Asutosh College':'http://www.asutoshcollege.in/','Bangabasi College':'http://bangabasi.org/home.php','Barrackpore Rastraguru Surendranath College':'http://www.brsnc.org/','Basanti Devi College':'http://www.basantidevicollege.edu.in/'}

yy=['Y','Yes','YES','y']
nn=['N','NO','no','No','n']
i=0
while i<len(Cn):
    print 'College Name : ',Cn[i]
    a=raw_input('Do u want to visit the website ? (Y/N) :')
    if a in yy:
        webbrowser.open(Cnw[Cn[i]])
    #elif a in nn:
    #   break
    i=i+1

这给了我以下输出:

College Name :  Acharya Girish Chandra Bose College
Do u want to visit the website ? (Y/N) :n
College Name :  AJC Bose College
Do u want to visit the website ? (Y/N) :n
College Name :  Ananda Mohan College
Do u want to visit the website ? (Y/N) :n
College Name :  Asutosh College
Do u want to visit the website ? (Y/N) :n
College Name :  Bangabasi College
Do u want to visit the website ? (Y/N) :n
College Name :  Barrackpore Rastraguru Surendranath College
Do u want to visit the website ? (Y/N) :n
College Name :  Basanti Devi College
Do u want to visit the website ? (Y/N) :n
>>> ================================ RESTART ================================
>>> 
College Name :  Acharya Girish Chandra Bose College
Do u want to visit the website ? (Y/N) :y
College Name :  AJC Bose College
Do u want to visit the website ? (Y/N) :y
College Name :  Ananda Mohan College
Do u want to visit the website ? (Y/N) :y
College Name :  Asutosh College
Do u want to visit the website ? (Y/N) :y
College Name :  Bangabasi College
Do u want to visit the website ? (Y/N) :y
College Name :  Barrackpore Rastraguru Surendranath College
Do u want to visit the website ? (Y/N) :y
College Name :  Basanti Devi College
Do u want to visit the website ? (Y/N) :y
>>>