根据 for 循环中的迭代次数向列表添加不同的值
Posted
技术标签:
【中文标题】根据 for 循环中的迭代次数向列表添加不同的值【英文标题】:Adding a different value to a list based on number of iteration in a for loop 【发布时间】:2022-01-19 20:25:25 【问题描述】:我是 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。
例如
index | platinum | quantity | ... | items name (problematic column) |
---|---|---|---|---|
1 | 10 | 1 | ... | melee_riven_mod_(veiled) |
2 | 11 | 1 | ... | melee_riven_mod_(veiled) |
3 | 12 | 2 | ... | zaw_riven_mod_(veiled) |
4 | ... | ... | ... | zaw_riven_mod_(veiled) |
但项目名称列对于所有行具有相同的名称,如下所示:
index | platinum | quantity | ... | items name (problematic column) |
---|---|---|---|---|
1 | 10 | 1 | ... | melee_riven_mod_(veiled) |
2 | 11 | 1 | ... | melee_riven_mod_(veiled) |
3 | 12 | 2 | ... | melee_riven_mod_(veiled) |
4 | ... | ... | ... | melee_riven_mod_(veiled) |
所以我想问一下我在 for 循环中做错了什么?它迭代 2 次,这是 url_list
中的 url 数量,但它不会更改项目的名称。
我没看到什么?
【问题讨论】:
当我复制你的代码时,它会引发一个错误。直接复制运行的时候有没有? 我无法运行您的代码。如果你修好了,我可以帮你。 抱歉,我遇到了缩进问题。现在应该修复了 【参考方案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]
到这里:
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
并在每次迭代时提供项目 和 其索引:
for iteration, url in enumerate(url_list):
....
【讨论】:
以上是关于根据 for 循环中的迭代次数向列表添加不同的值的主要内容,如果未能解决你的问题,请参考以下文章