【问题标题】:How to use 2 "urlopen" in the same cell in jupyter notebook?如何在 jupyter notebook 的同一个单元格中使用 2 个“urlopen”?
【发布时间】:2019-08-21 17:37:20
【问题描述】:

我想在 jupyter notebook 的同一个单元格中有 2 个“带有 urllib.request.urlopen”的请求。第一个给出第二个的 nextpagetoken。

如果我将两个请求分开在两个单元格中,则两者都可以工作。

如果两个请求在同一个单元格中,则第二个请求以状态结束:"INVALID REQUEST"

我的第一个想法是,第一个 urllib.request.urlopen 需要以某种方式关闭,以便在一个单元格中有多个请求,但我没有找到解决问题的解决方案。

将两个请求分隔在两个单元格中 --> 有效,但我需要在 1 个单元格中

尝试close()第一个请求 --> 没有变化

#open first link to build list of place_ids
with urllib.request.urlopen(LINK) as y:
    x_dict = json.loads(y.read().decode())
    next_page_token = x_dict["next_page_token"]
    #potential solution that does not work: y.close()

#open second link with pagetoken
with urllib.request.urlopen(LINK WITH NEXTPAGETOKEN) as y_page2:
    x_dict_page_2=json.loads(y_page2.read().decode())
    print(x_dict_page_2)

预期输出:第二个 urllib.request.urlopen 给出结果

实际输出:第二个 urllib.request.urlopen 给出:

{'html_attributions': [], 'results': [], 'status': 'INVALID_REQUEST'}

【问题讨论】:

    标签: python python-3.x python-requests jupyter-notebook urllib


    【解决方案1】:

    添加

    time.sleep()

    在两个请求之间,它将起作用

    【讨论】:

      猜你喜欢
      • 2018-10-31
      • 2022-08-10
      • 1970-01-01
      • 1970-01-01
      • 2017-12-13
      • 1970-01-01
      • 2017-11-16
      • 2018-01-08
      • 1970-01-01
      相关资源
      最近更新 更多