0

我正在尝试使用此处找到的 Hacker News API ,尤其是实时数据部分。

我目前正在尝试打印从/v0/maxitemAPI 获得的每个新项目的响应。

下面给出的是我目前拥有的代码:

import pyrebase
from config import config
import requests

firebase = pyrebase.initialize_app(config)
firebase_db = firebase.database()

_BASEURL_ = "https://hacker-news.firebaseio.com/v0/item/"

def print_response(id):
    headers = {"Content-Type": "application/json"}
    print(_BASEURL_ + str(id) + ".json")
    response = requests.get(_BASEURL_ + str(id) + ".json", headers=headers)
    print(response.content)


def new_post_handler(message):
    print(message["data"])
    print_response(message["data"])

my_stream = firebase_db.child("/v0/maxitem").stream(new_post_handler,
                                                    stream_id="new_posts")

我能够在第一次requests.get运行时获得有效响应。但是第二次,我总是得到NULL响应内容的值。

但是,该GETURL 可以在邮递员上使用,能够在那里获得有效的响应。问题似乎特别在于requests模块如何第二次处理 URL。

非常感谢任何帮助。

4

0 回答 0