gpt4 book ai didi

python - 根据 for 循环中的迭代次数向列表添加不同的值

转载 作者:行者123 更新时间:2023-12-05 05:51:21 31 4
gpt4 key购买 nike

我是 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。

例如

<表类="s-表"><头>索引白金数量<日>... 项目名称(有问题的列)<正文>1101...melee_riven_mod_(隐藏)2111...melee_riven_mod_(隐藏)3122...zaw_riven_mod_(隐藏)4.........zaw_riven_mod_(隐藏)

但项目名称列对所有行具有相同的名称,如下所示:

<表类="s-表"><头>索引白金数量<日>... 项目名称(有问题的列)<正文>1101...melee_riven_mod_(隐藏)2111...melee_riven_mod_(隐藏)3122...melee_riven_mod_(隐藏)4.........melee_riven_mod_(隐藏)

所以我想问问我在for循环中做错了什么?它迭代了 2 次,这是 url_list 中的 url 数量。但它不会更改项目的名称。我没有看到什么?

最佳答案

改变

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]

对此:

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 并递增它,它同时提供item 它在每次迭代时的索引:

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

关于python - 根据 for 循环中的迭代次数向列表添加不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70384219/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com