我对f:loop python和文件中的for单词有问题

2024-10-02 14:27:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经编写了一个简单的程序,通过一个非常昂贵的csgo标签列表,打印出API响应中发现的标签名称。我不知道为什么我的程序只显示列表中最后一个标签的名称

import json
import requests
import time

url = "http://steamcommunity.com/profiles/76561198231636540/inventory/json/730/2"

params = {
    'l': 'english',
    'key': '-------------------------',
    'steamid': 76561198231636540
}    

r = requests.get(url=url, params=params)

paczka_json = r.json()

paczka_des_dict = paczka_json["rgDescriptions"]

paczka_des_list = list(paczka_des_dict)

j = 0

for element in paczka_des_dict:
    linib = paczka_des_list[j]
    j += 1

with open(r"C:\Users\micha\Desktop\Visual_projects\steam_inventory\stickers.txt", "r") as f: 
    stickers = paczka_des_dict[linib]['descriptions'][6]['value']
    for words in f:
        if (words in stickers):
            print(words)

清单包括:

compLexity Gaming | Katowice 2014
HellRaisers | Katowice 2014
Crown (Foil) 
Clan-Mystik | Katowice 2014
Flammable (Foil)

我正在处理的答复是:

Sticker: Easy Peasy, Crown (Foil), Flammable (Foil), Easy Peasy

我不知道为什么我的代码只返回可燃物(箔片)和跳过皇冠(箔片)

当我将冕(箔)与易燃(箔)切换时,只有冕(箔)显示

有什么想法吗


更详细的蒸汽API响应(标签在末尾)

<br><div id="sticker_info" name="sticker_info" title="Sticker" style="border: 2px solid rgb(102, 102, 102); border-radius: 6px; width=100; margin:4px; padding:8px;"><center><img width=64 height=48 src="https://steamcdn-a.akamaihd.net/apps/730/icons/econ/stickers/standard/lemon.a8860b6b7a01e608a7b10b559a38ee82cb197e3b.png"><img width=64 height=48 src="https://steamcdn-a.akamaihd.net/apps/730/icons/econ/stickers/stickers2/crown_foil.77c38fe60426ee084fd5c8fec0c680c342e05743.png"><img width=64 height=48 src="https://steamcdn-a.akamaihd.net/apps/730/icons/econ/stickers/community01/flammable_foil.f4741f611960bfb7d236177871bd00f7b052bdd5.png"><img width=64 height=48 src="https://steamcdn-a.akamaihd.net/apps/730/icons/econ/stickers/standard/lemon.a8860b6b7a01e608a7b10b559a38ee82cb197e3b.png"><br>Sticker: Easy Peasy, Crown (Foil), Flammable (Foil), Easy Peasy</center></div>

在某种程度上重要的整个API响应

{
  "success": true,
  "rgInventory": {
    "19057393402": {
      "id": "19057393402",
      "classid": "3907426036",
      "instanceid": "188530139",
      "amount": "1",
      "pos": 1
    },
  },
  "rgCurrency": [],
  "rgDescriptions": {
    "3907426036_188530139": {
      "appid": "730",
      "classid": "3907426036",
      "instanceid": "188530139",
      "icon_url": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot621FAR17PLfYQJD_9W7m5a0n_L1JaKfzzoGuJJ02e2W8d6m2gztrkRoZmigItDGcgA_N1iFqwC-xr_m1J-57YOJlyVerprbwA",
      "icon_url_large": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot621FAR17PLfYQJD_9W7m5a0n_L1JaLummpD78A_3rmTodTwiwzkqUNoN236cteWcwFtY13RqADql7q8h5PttZzJwHdgvz5iuyif2YhKbw",
      "icon_drag_url": "",
      "name": "AWP | Asiimov",
      "market_hash_name": "AWP | Asiimov (Battle-Scarred)",
      "market_name": "AWP | Asiimov (Battle-Scarred)",
      "name_color": "D2D2D2",
      "background_color": "",
      "type": "Covert Sniper Rifle",
      "tradable": 0,
      "marketable": 1,
      "commodity": 0,
      "market_tradable_restriction": "7",
      "cache_expiration": "2020-07-19T07:00:00Z",
      "fraudwarnings": [
        "Name Tag: ''\u4f60\u662f\u6211\u7684\u5a73\u794e\u5b9d\u8d1d''"
      ],
      "descriptions": [
        {
          "type": "html",
          "value": "Exterior: Battle-Scarred"
        },
        {
          "type": "html",
          "value": " "
        },
        {
          "type": "html",
          "value": "High risk and high reward, the infamous AWP is recognizable by its signature report and one-shot, one-kill policy. It has been custom painted with a sci-fi design.\n\n<i>Anyone can 
predict the future... a visionary shapes it</i>"
        },
        {
          "type": "html",
          "value": " "
        },
        {
          "type": "html",
          "value": "The Phoenix Collection",
          "color": "9da1a9",
          "app_data": {
            "def_index": "65535",
            "is_itemset_name": 1
          }
        },
        {
          "type": "html",
          "value": " "
        },
        {
          "type": "html",
          "value": "<br><div id=\"sticker_info\" name=\"sticker_info\" title=\"Sticker\" style=\"border: 2px solid rgb(102, 102, 102); border-radius: 6px; width=100; margin:4px; padding:8px;\"><center><img width=64 height=48 src=\"https://steamcdn-a.akamaihd.net/apps/730/icons/econ/stickers/berlin2019/sig_aerial_gold.b93441810325923985b5a30cafad702beb36f4ea.png\"><img width=64 height=48 src=\"https://steamcdn-a.akamaihd.net/apps/730/icons/econ/stickers/cologne2015/envyus.89a88946df01bd6eb9a7e9dc02885ec043671702.png\"><br>Sticker: Aerial (Gold) | Berlin 2019, Team EnVyUs | Cologne 2015</center></div>",
          "app_data": {
            "def_index": "65535"
          }
        }
      ],
    },

Tags: namehttpssrcurlimgvaluehtmltype
2条回答
sticker_response = paczka_des_dict[linib]['descriptions'][6]['value'] #to jest string napewno

with open(r"C:\Users\micha\Desktop\Visual_projects\steam_inventory\stickers.txt", "r") as f: #popraw
    for line in f:
        line2 = line.rstrip()
        if(line2 in sticker_response):
            print(line, end='')

谢谢你,斯托夫,你是对的

我认为您在字符串比较方面有问题,或者您没有解析出足够的信息

# process your HTML into a list 
sticker_line = 'Sticker: Easy Peasy, Crown (Foil), Flammable (Foil), Easy Peasy'
stickers = set(s.strip() for s in sticker_line.split(':')[1].split(',')) 

with open(sticker_file) as f:
    for line in f:
        parts = [s.strip() for s in line.rstrip().split('|')]  # separate each line 
        if parts[0] in stickers:   # compare your items
            print(parts[0]) 

相关问题 更多 >