2017-08-04 78 views
-1

当我尝试发布的请求我得到这个错误,我只是尝试添加例外,但是例外不能处理它Python的请求例外

 

    raise ConnectionError(e, request=request) 
ConnectionError: HTTPSConnectionPool(host='www.examplewebsite.com', port=443): Max retries exceeded with url: /login/ (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond',)) 

我已经尝试了本

 
except requests.exceptions.ConnectionError as C: 
    print "Failed" 
except requests.exceptions.TooManyRedirects: 
    print "Failed" 

但还是同样的错误

什么办法解决呢?

编辑:

 
def job(lk): 
    try: 
     session = requests.session() 

     headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0'} 
     r = session.get("https://www.example.com/login/") 
     post = session.post('https://www.example.com', data={"log": "name", 
                     "pwd": "name",}, 
                     headers=headers) 
     rGet = session.get('https://www.example.com') 


    except requests.exceptions.ConnectionError as C: 
     print "Failed 2" 
    except requests.exceptions.TooManyRedirects: 
     print "Failed 3" 
+0

能告诉你多一点你的代码吗?大部分与try/catch相关的部分,它可以帮助,谢谢! –

+0

问题中添加的代码 请检查 –

+0

@GreyByte现在,您在_creating(定义)函数期间捕获异常。如果你想在function_的_execution过程中捕获异常,可以尝试/ except块应该定义_within_函数体。 –

回答

0

你的try/except块是不是在正确的地方,它必须是功能:

def job(lk): 
    session = requests.session() 
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0'} 
    try: 

     r = session.get("https://www.example.com/login/") 
     post = session.post(
      'https://www.example.com', 
      data={"log": "name", "pwd": "name",},                 
      headers=headers 
      ) 
     rGet = session.get('https://www.example.com') 


    except requests.exceptions.ConnectionError as C: 
     print "Failed 2" 
    except requests.exceptions.TooManyRedirects: 
     print "Failed 3" 

另外请注意,当前的异常处理会不是非常有用的故障排除 - 你没有确切的异常,完整的追溯,你甚至不知道哪三个请求失败 - 但我认为这只是占位符代码ATM。

+0

是的,它在功能 只是一个错误,而在这里发布 –

+0

@GreyByte如果你希望得到任何帮助,请发布正确的代码和完整的异常消息,追踪等。 –

0

try catch块应该是里面的功能。

def job(lk): 
    try: 
     session = requests.session() 

     headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0'} 
     r = session.get("https://www.example.com/login/") 
     post = session.post('https://www.example.com', data={"log": "name", 
                     "pwd": "name",}, 
                     headers=headers) 
     rGet = session.get('https://www.example.com') 


    except requests.exceptions.ConnectionError: 
     print("Failed 2") 
    except requests.exceptions.TooManyRedirects: 
     print("Failed 3") 
+0

try/catch在函数内 只是一个错误,而粘贴在这里 –

0

正如布鲁诺说,在try /除应该是函数(或周围调用该函数的代码包裹)的内部。

的原因是,当函数被调用,运行发生异常;而你的try/except块,因为它只捕获错误,同时声明函数,而不是在运行时。

如果你不理解上的差异,你应该尝试更好地了解功能,在电子书/文档/当然,你用它来学习Python是如何工作的。

+0

try/catch在函数内 它只是一个错误,而在这里发布 –

0

也许你没有得到你认为你所得到的例外。与通用except运行你的代码,以确定异常的类型,这将引发什么:

except Exception as exc: 
    print(type(exc), str(exc))