2016-06-12 89 views
0

我試圖獲取包括3XX的http狀態代碼,但是從我的代碼中我無法打印它。要在Python 3(urllib)中打印http狀態代碼

下面是代碼:

import urllib 
import urllib.request 
import urllib.error 

urls = ['http://hotdot.pro/en/404/', 'http://www.google.com', 'http://www.yandex.ru', 'http://www.python.org', 'http://www.voidspace.org.uk'] 
fh = open("example.txt", "a") 
def getUrl(urls): 
    for url in urls: 
     try: 
      with urllib.request.urlopen(url) as response: 
       requrl = url 
       the_page = response.code 
       fh.write("%d, %s\n" % (int(the_page), str(requrl))) 
     except (urllib.error.HTTPError, urllib.error.URLError) as e: 
      requrl = url 
      print (e.code) 
      fh.write("%d, %s\n" % (int(e.code), str(requrl))) 
getUrl(urls) 

有人可以幫助我?

+0

是你真正的問題:如何禁用重定向? (這樣'urlopen()'不會自動跟隨任何30x重定向?) – jfs

+0

是的,我不希望url被重定向。只需打印響應代碼和響應時間即可。 – arjun9916

+0

請參閱[有沒有簡單的方法來請求在Python中的URL,而不是遵循重定向?](http://stackoverflow.com/q/110498/4279) – jfs

回答

3

並非所有類URLError的錯誤都將有code,有些將只有reason

此外,在同一except塊醒目URLErrorHTTPError是不是一個好主意(見docs):

def getUrl(urls): 
    for url in urls: 
     try: 
      with urllib.request.urlopen(url) as response: 
       requrl = url 
       the_page = response.code 
       print(the_page) 
       fh.write("%d, %s\n" % (int(the_page), str(requrl))) 
     except urllib.error.HTTPError as e: 
      requrl = url 
      print(e.code) 
      fh.write("%d, %s\n" % (int(e.code), str(requrl))) 
     except urllib.error.URLError as e: 
      if hasattr(e, 'reason'): 
       print(e.reason) 
       fh.write("%s, %s\n" % (e.reason, str(requrl))) 
      elif hasattr(e, 'code'): 
       print(e.code) 
       fh.write("%d, %s\n" % (int(e.code), str(requrl))) 
+0

我仍然無法獲得3XX http響應。 。 – arjun9916