2017-05-14 4 views
0

urllib.request 라이브러리를 사용하여 사이트의 컨텐츠를 가져올 때 사이트에 연결 오류가 있거나 다음과 같은 오류 메시지가 표시됩니다. 존재하지urllib 라이브러리를 사용할 때 결과 페이지가 없을 때 다시 연결하는 방법

urllib.error.URLError: <urlopen error [WinError 10060]> 

내 코드는 내가 오히려 자동 종료보다 연결을 잃은 후 다시 연결할 수 있도록 할 방법이

import urllib.request 
while True: 
response = urllib.request.urlopen('http://192.168.198.129:8080/search.txt') 
html = response.read().decode("gb2312") 
print(html) 

같다.

THX! 파이썬에서

답변

1

는 예외를 처리 할 수있는 표준 방법은 use try statement하는 것입니다

while True: 
    try: 
     response = urllib.request.urlopen('http://192.168.198.129:8080/search.txt') 
     html = response.read().decode("gb2312") 
     print(html) 
    except URLError as e: 
     // code to execute in case of error 
     print("That was an error :(") 
     print(e) # should print out more information about the error 
+0

대단히 감사합니다! –