[英]Why can't I append a dictionary index for one item but I can for any other item?
# Import libraries
from bs4 import BeautifulSoup
import requests
import pandas as pd
import time
import ast
start_time = time.time()
s = requests.Session()
#Get URL and extract content
page=1
traits = []
accessories, backgrounds, shoes = [], [], []
while page != 100:
params = {
('arg', f"Qmer3VzaeFhb7c5uiwuHJbRuVCaUu72DcnSoUKb1EvnB2x/{page}"),
}
content = s.get('https://ipfs.infura.io:5001/api/v0/cat', params=params, auth=('', ''))
soup = BeautifulSoup(content.text, 'html.parser')
page = page + 1
traits = ast.literal_eval(soup.text)['attributes']
df = pd.DataFrame(traits)
df1 = df[df['trait_type']=='ACCESSORIES']
accessories.append(df1['value'].values[0])
Can anyone explain to my what I'm doing wrong?谁能向我解释我做错了什么? When I run the above code I get the following error:
当我运行上面的代码时,我收到以下错误:
IndexError: index 0 is out of bounds for axis 0 with size 0
But whenever I use a different index like BACKGROUNDS or SHOES instead of ACCESSORIES, like the following code, I don't get the above error and it runs perfectly.但是每当我使用不同的索引(如 BackgroundS 或 SHOES 而不是附件)时,就像下面的代码一样,我不会收到上述错误并且它运行得很好。
# Import libraries
from bs4 import BeautifulSoup
import requests
import pandas as pd
import time
import ast
start_time = time.time()
s = requests.Session()
#Get URL and extract content
page=1
traits = []
accessories, backgrounds, shoes = [], [], []
while page != 100:
params = {
('arg', f"Qmer3VzaeFhb7c5uiwuHJbRuVCaUu72DcnSoUKb1EvnB2x/{page}"),
}
content = s.get('https://ipfs.infura.io:5001/api/v0/cat', params=params, auth=('', ''))
soup = BeautifulSoup(content.text, 'html.parser')
page = page + 1
traits = ast.literal_eval(soup.text)['attributes']
df = pd.DataFrame(traits)
df1 = df[df['trait_type']=='BACKGROUND']
backgrounds.append(df1['value'].values[0])
Can anyone here help me figure out what I'm doing different or wrong between the two code?这里有人能帮我弄清楚我在两个代码之间做的不同或错误吗?
PS When running either code up until the append line, both BACKGROUND and ACCESSORIES are listed in df and df1. PS 当运行任一代码直到追加行时,背景和附件都列在 df 和 df1 中。 Only when I add the append line does the ACCESSORIES index dissapear, but this doesn't happen for BACKGROUND or SHOES.
只有当我添加 append 行时,ACCESSORIES 索引才会消失,但对于背景或鞋子不会发生这种情况。
Following code solves the issue:下面的代码解决了这个问题:
# Import libraries
from bs4 import BeautifulSoup
import requests
import pandas as pd
import time
import ast
start_time = time.time()
s = requests.Session()
#Get URL and extract content
page=1
traits = []
accessories, backgrounds, shoes = [], [], []
while page != 5556:
params = {
('arg', f"Qmer3VzaeFhb7c5uiwuHJbRuVCaUu72DcnSoUKb1EvnB2x/{page}"),
}
content = s.get('https://ipfs.infura.io:5001/api/v0/cat', params=params, auth=('', ''))
soup = BeautifulSoup(content.text, 'html.parser')
page = page + 1
traits = ast.literal_eval(soup.text)['attributes']
df = pd.DataFrame(traits)
df1 = df[df['trait_type']=='ACCESSORIES']
try:
accessories.append(df1['value'].values[0])
except:
pass
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.