【问题标题】:'urllib' on an infinite loop'urllib' 无限循环
【发布时间】:2020-02-19 18:29:10
【问题描述】:

一段时间以来,我一直在尝试使用 cloud9 来帮助我处理工作中的重复性任务。然而,这些任务中的大多数都涉及网络抓取,我很难使用 urllib 库。

即使是简单的代码也不起作用。它一直在运行,我找不到原因。我会很感激一些提示...

from urllib.request import urlopen
import json
import ssl

# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

url = ('Enter - ')
html = urlopen(url, context=ctx).read()
str_data = open(html).read()
json_data = json.loads(str_data)

for entry in json_data:

name = entry[0];
    title = entry[1];
    print((name, title))

【问题讨论】:

    标签: python amazon-web-services aws-cloud9


    【解决方案1】:

    如果不提供太多调试信息,只能假设您可能正在超时,因为您没有为 urlopen 函数提供超时。

    为避免这种情况,请为您的 urlopen 函数设置一个合理的超时时间,使其不会无限期地运行。

    【讨论】:

    • 亚历克斯,你是对的。包含超时功能是一种切割动作。
    【解决方案2】:

    帮自己一个忙,将其替换为Requests。您的示例简化为:

    import requests
    
    url = "..."
    json_data = requests.get(url).json()
    
    for entry in json_data:
        name = entry[0]
        title = entry[1]
        print(name, title)
    

    这也消除了代码中大量错误的可能性。

    【讨论】:

    • 我听从了你的建议,但不幸的是,我遇到了同样的问题。
    【解决方案3】:

    新代码

    import requests
    import json
    
    url = "-"
    json_data = requests.get(url).json()
    info = json.loads(json_data)
    
    print('User count:', len(info))
    print(info)
    

    调试模式:

    [IKP3db-g] 15:43:52,971733 - INFO - IKP3db 1.4.1 - Inouk Python Debugger for CPython 3.6+
    [IKP3db-g] 15:43:52,972714 - INFO - IKP3db listening on 127.0.0.1:15471
    [IKP3db-g] 15:43:53,003016 - INFO - Connected with 127.0.0.1:50936
    Traceback (most recent call last):
      File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/connection.py", line 157, in _new_conn
        (self._dns_host, self.port), self.timeout, **extra_kw
      File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/util/connection.py", line 84, in create_connection
        raise err
      File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/util/connection.py", line 74, in create_connection
        sock.connect(sa)
    TimeoutError: [Errno 110] Connection timed out
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 672, in urlopen
        chunked=chunked,
      File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 376, in _make_request
        self._validate_conn(conn)
      File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 994, in _validate_conn
        conn.connect()
      File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/connection.py", line 334, in connect
        conn = self._new_conn()
      File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/connection.py", line 169, in _new_conn
        self, "Failed to establish a new connection: %s" % e
    urllib3.exceptions.NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x7fa2bb9729e8>: Failed to establish a new connection: [Errno 110] Connection timed out
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/home/ubuntu/.local/lib/python3.6/site-packages/requests/adapters.py", line 449, in send
        timeout=timeout
      File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 720, in urlopen
        method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
      File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/util/retry.py", line 436, in increment
        raise MaxRetryError(_pool, url, error or ResponseError(cause))
    urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='ot.cloud.mapsfinancial.com', port=443): Max retries exceeded with url: /pegasus/api/consulta/caixa/saldos/carteira/DVG1%20FIA/2018-01-08 (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7fa2bb9729e8>: Failed to establish a new connection: [Errno 110] Connection timed out',))
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/usr/local/lib/python3.6/dist-packages/ikp3db.py", line 2047, in main
      File "/usr/local/lib/python3.6/dist-packages/ikp3db.py", line 1526, in _runscript
        exec(statement, globals, locals)
      File "<string>", line 1, in <module>
      File "/home/ubuntu/environment/Testing.py", line 5, in <module>
        json_data = requests.get(url).json()
      File "/home/ubuntu/.local/lib/python3.6/site-packages/requests/api.py", line 75, in get
        return request('get', url, params=params, **kwargs)
      File "/home/ubuntu/.local/lib/python3.6/site-packages/requests/api.py", line 60, in request
        return session.request(method=method, url=url, **kwargs)
      File "/home/ubuntu/.local/lib/python3.6/site-packages/requests/sessions.py", line 533, in request
        resp = self.send(prep, **send_kwargs)
      File "/home/ubuntu/.local/lib/python3.6/site-packages/requests/sessions.py", line 646, in send
        r = adapter.send(request, **kwargs)
      File "/home/ubuntu/.local/lib/python3.6/site-packages/requests/adapters.py", line 516, in send
        raise ConnectionError(e, request=request)
    requests.exceptions.ConnectionError: HTTPSConnectionPool(host='ot.cloud.mapsfinancial.com', port=443): Max retries exceeded with url: xxxxxxxx (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7fa2bb9729e8>: Failed to establish a new connection: [Errno 110] Connection timed out',))[IKP3db-g] 15:46:04,668520 - INFO - Uncaught exception. Entering post mortem debugging
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-25
      • 1970-01-01
      • 1970-01-01
      • 2013-09-05
      • 1970-01-01
      相关资源
      最近更新 更多