Python 3.4.3中的错误URL

时间:2017-05-29 21:51:57

标签: python python-3.x urllib

我是新手,所以请帮助我。我正在使用urllib.request来打开和阅读网页。有人能告诉我我的代码如何处理重定向,超时,格式错误的URL? 我有点找到超时的方法,但我不确定它是否正确。是吗?欢迎所有意见!这是:

from socket import timeout
import urllib.request
try:
            text = urllib.request.urlopen(url, timeout=10).read().decode('utf-8')
except (HTTPError, URLError) as error:
            logging.error('Data of %s not retrieved because %s\nURL: %s', name, error, url)
except timeout:
            logging.error('socket timed out - URL %s', url)

请帮助我,因为我是新手。谢谢!

2 个答案:

答案 0 :(得分:0)

查看urllib error page

对于以下行为:

  • 重定向:HTTP代码302,因此这是带有代码的HTTPError。您也可以使用HTTPRedirectHandler而不是失败。
  • 超时:你有正确的。
  • 格式错误的网址:这是一个URLError。

以下是我将使用的代码:

from socket import timeout
import urllib.request
try:
    text = urllib.request.urlopen("http://www.google.com", timeout=0.1).read()
except urllib.error.HTTPError as error:
    print(error)
except urllib.error.URLError as error:
    print(error)
except timeout as error:
    print(error)

我无法找到重定向网址,因此我不确定如何检查HTTPError是否为重定向。

您可能会发现requests包更容易使用(在urllib页面上建议)。

答案 1 :(得分:0)

使用DECLARE @StartDate = DateTime.Parse("1979-03-31"); @numbers_10 = SELECT * FROM (VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9) ) AS T(Value); @numbers_100 = SELECT (a.Value*10 + b.Value) AS Value FROM @numbers_10 AS a CROSS JOIN @numbers_10 AS b; @numbers_10000 = SELECT (a.Value*100 + b.Value) AS Value FROM @numbers_100 AS a CROSS JOIN @numbers_100 AS b; @result = SELECT Value, @StartDate.AddDays( Value ) AS Date FROM @numbers_10000; OUTPUT @result TO "/res.csv" USING Outputters.Csv(outputHeader:true); 包我能找到更好的解决方案。您需要处理的唯一例外是:

requests

要获取该页面的文本,您需要做的就是: try: r = requests.get(url, timeout =5) except requests.exceptions.Timeout: # Maybe set up for a retry, or continue in a retry loop except requests.exceptions.TooManyRedirects as error: # Tell the user their URL was bad and try a different one except requests.exceptions.ConnectionError: # Connection could not be completed except requests.exceptions.RequestException as e: # catastrophic error. bail.