将 Json 文件中的键与 python 中列表中的值进行比较 [已关闭]

Comparing keys in a Json file with values in a List In python [closed]

提问人:Nick_Sutton 提问时间:7/29/2023 更新时间:7/29/2023 访问量:25

问:


想改进这个问题吗?通过编辑这篇文章添加详细信息并澄清问题。

4个月前关闭。

3 个月前起,社区正在审查是否重新讨论这个问题。

我需要将此 Json 文件中的标题与列表中的标题进行比较。我的目标是检查哪些标题等于此列表的值,然后打印有关该标题的信息。例如,如果其中一个标题是 Bloons TD 6,我只想打印相应的描述和 viewableDate。

这是我到目前为止所拥有的

response = requests.get("https://store-site-backend-static-ipv4.ak.epicgames.com/freeGamesPromotions?locale=en-US&country=US&allowCountries=US")

freeGameData = json.loads(response.text)
freeNowList = Epic_Scraper()

#freeNowList returns ['Bloons TD 6', 'Loop Hero']
#freeGameData returns the json listed below

Json 文件如下所示

{
    "data": {
        "Catalog": {
            "searchStore": {
                "elements": [
                    {
                        "title": "Loop Hero",
                        "id": "db122f42b41f4dec927e0f280cf653bd",
                        "namespace": "ff50f85ed609454e80ac46d9496da34d",
                        "description": "The Lich has thrown the world into a timeless loop and plunged its inhabitants into never ending chaos. Wield an expanding deck of mystical cards to place enemies, buildings, and terrain along each unique expedition loop for the brave hero.",
                        "effectiveDate": "2021-03-04T18:00:00.000Z",
                        "offerType": "BASE_GAME",
                        "expiryDate": null,
                        "viewableDate": "2021-03-04T18:00:00.000Z",
                        "status": "ACTIVE",
                        "isCodeRedemptionOnly": false,
                    },
                    {
                        "title": "Bloons TD 6",
                        "id": "b27e3b556f1048b9824c7196f32afceb",
                        "namespace": "6a8dfa6e441e4f2f9048a98776c6077d",
                        "description": "The Bloons are back and better than ever! Get ready for a massive 3D tower defense game designed to give you hours and hours of the best strategy gaming available.",
                        "effectiveDate": "2022-07-19T12:00:00.000Z",
                        "offerType": "BASE_GAME",
                        "expiryDate": null,
                        "viewableDate": "2022-04-30T00:00:00.000Z",
                        "status": "ACTIVE",
                        "isCodeRedemptionOnly": false,
                    },
                    {
                        "title": "Destiny 2: Bungie 30th Anniversary Pack",
                        "id": "e7b9e222c7274dd28714aba2e06d2a01",
                        "namespace": "428115def4ca4deea9d69c99c5a5a99e",
                        "description": "The 30th Anniversary Pack includes a new Dungeon, Gjallarhorn Exotic Rocket Launcher, new weapons, armor, and much more.                    ",
                        "effectiveDate": "2022-08-23T13:00:00.000Z",
                        "offerType": "DLC",
                        "expiryDate": null,
                        "viewableDate": "2022-08-08T15:00:00.000Z",
                        "status": "ACTIVE",
                        "isCodeRedemptionOnly": false,
                        },
                    },

}
python json 循环 字典 web-scraping

评论

0赞 Barmar 7/29/2023
你有什么问题?遍历列表。测试标题是否在列表中,如果是,请打印所需的内容。这是一个简单的测试。elementsif something in list:
0赞 7/29/2023
您发布的尝试代码不包括任何解析 json 文件和/或比较数据的尝试。请考虑通过以下方式在问题上投入更多资金 如何提问

答:

0赞 not_speshal 7/29/2023 #1

循环浏览 json 的最内层列表,仅当标题在列表中时才打印信息:

for e in freeGameData["data"]["Catalog"]["searchStore"]["elements"]:
    if e["title"] in freeNowList:
        print(e["description"])
        print(e["viewableDate"]+"\n")