我是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(秘而不宣) |