繁体   English   中英

如何在jupyter笔记本中的同一单元格中使用2“urlopen”?

[英]How to use 2 “urlopen” in the same cell in jupyter notebook?

我想在jupyter笔记本中的同一个单元格中有2个“带urllib.request.urlopen”请求。 第一个为第二个提供nextpagetoken。

如果我在两个单元格中分离这两个请求都可以工作。

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

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

在两个单元格中分隔两个请求 - > Works,但我需要在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'}

time.sleep()

在这两个请求之间它会起作用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM