2016-05-01 38 views
-3

我写这个剧本错误在我的脚本下载?

import urllib 
urllib.urlretrieve("URL","path\ name.jpg") 

它的工作 但是,如果没有互联网它做出错误的 我想,如果没有互联网。等待通过互联网进行连接,然后再工作

+0

你的问题很不清楚。试着总是提供你所做的事情(包括这些),发生了什么(缺少)以及你期望发生什么(不清楚)。 –

回答

0

你可以写这样的事情:

def wait_for_internet_connection(): 
    while True: 
     try: 
      response = urllib2.urlopen('http://google.com',timeout=1) 
      return 
     except urllib2.URLError: 
      pass 

def main(): 
    #your code here 

wait_for_internet_connection() 
main() 

while循环将执行,直到有一个有效的互联网连接,然后执行你的代码。