根据 for 循环的迭代次数,将不同的值添加到列表中

3

我是Python和编程方面的新手,我在一个网站解析项目中遇到了困难。

这是我所写的代码:

import requests
from bs4 import BeautifulSoup
import pandas as pd
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
pd.set_option('display.max_colwidth', -1)
import json

#necessary lists
url_list = [
    "https://warframe.market/items/melee_riven_mod_(veiled)",
    "https://warframe.market/items/zaw_riven_mod_(veiled)"
    ]
item_list = []
items_name = []
combined_data = []
iteration = 0


#looping for every url found in url_list
for url in url_list:
    #requesting data
    r = requests.get(url)
    soup = BeautifulSoup(r.content, "html.parser")

    #splitting the last part of the url which has the name of the item that I want to insert in the dataframe
    name = url.split("/")[4]
    items_name.append(name)

    #Finding in the parsed HTML code where the JSON file starts ( it start from <script> n°2)
    results = soup.find_all('script')[2].text.strip()
    data = json.loads(results)
    combined_data.append(data) #combining all the data into one list


    #filtering only the users who sell the items and are either "ingame" or "online"
    for payload in combined_data[iteration]["payload"]["orders"]:
        if payload["order_type"] == "sell" and (payload["user"]["status"] == "online" or payload["user"]["status"] == "ingame"):
            p = payload
            item_list.append(p) 
            #adding the items names to the item list       ???? PROBLEM ?????
            item_list = [dict(item, **{'name':items_name[iteration]}) for item in item_list]
    #trying to change the list from where the data gets taken from and the items name        ????? PROBLEM ????
    iteration += 1        
    
    #creating a dataframe with all the values
    df = pd.DataFrame(item_list).sort_values(by=["platinum"])

我想做的事情是将url所指向的物品名称添加到item_list中。
例如:
索引 白金 数量 ... 物品名称(有问题的列)
1 10 1 ... 近战裂罅MOD(秘而不宣)
2 11 1 ... 近战裂罅MOD(秘而不宣)
3 12 2 ... zaw裂罅MOD(秘而不宣)
4 ... ... ... zaw裂罅MOD(秘而不宣)
但是物品名称的列对于所有行来说都是相同的,像这样:
索引 白金 数量 ... 物品名称(有问题的列)
1 10 1 ... 近战裂罅MOD(秘而不宣)
2 11 1 ... 近战裂罅MOD(秘而不宣)
3 12 2 ... 近战裂罅MOD(秘而不宣)
4 ... ... ... 近战裂罅MOD(秘而不宣)
所以我想问一下,在这个for循环中我错在哪里了?它迭代了2次,这是url_list中的url数量,但它没有改变物品名称。我看漏了什么?

1
当我复制你的代码时,它会引发一个错误。如果你直接复制并运行它,它会有同样的错误吗? - user17242583
1
我无法运行你的代码。如果你修复它,我可以帮助你。 - user17242583
1
抱歉,我的缩进有问题。现在应该已经修复了。 - Pattumazzo
1个回答

1

更改

if payload["order_type"] == "sell" and (payload["user"]["status"] == "online" or payload["user"]["status"] == "ingame"):
     p = payload
     item_list.append(p)
     #adding the items names to the item list       ???? PROBLEM ?????
     item_list = [dict(item, **{'name':items_name[iteration]}) for item in item_list]

To this:

if payload["order_type"] == "sell" and (payload["user"]["status"] == "online" or payload["user"]["status"] == "ingame"):
    payload['name'] = items_name[iteration]
    item_list.append(payload)

请注意,您可以使用enumerate循环遍历url_list,而不是拥有单独的变量iteration并对其进行递增。enumerate在每次迭代时提供了项它的索引:

for iteration, url in enumerate(url_list):
    ....

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接