简体   繁体   English

为什么我不能为一个项目附加字典索引但我可以为任何其他项目?

[英]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.

相关问题 为什么不能将字符串对象附加到字典 - Why can't I append a string object to a dictionary 为什么不能从字典中追加此值? - why can't I append this value from a dictionary? 如何将字符串项作为子列表项追加到列表中? - How can I append a string item as a sublist item in a list? 我无法通过索引类型从列表中删除该项目 - I can't remove the item from the list by the type of the index 如何将一个字典中的值作为项目合并到在“for”循环中创建的另一个字典? - How can I merge values in one dictionary as an item to another dictionary created inside a "for" loop? "找到列表中的项目,但是当我按索引询问位置时,它说找不到该项目" - Item in list found but when I ask for location by index it says that the item can't be found 如何将两个列表项附加到一个里面有两个项目的项目? (Python) - How can I append two list items to one item with two items inside? (Python) 如何打开文件并将项目附加到Python中的现有列表 - How can I open a file and append an item to an existing list in Python 我可以在Python中同时在同一个项目上使用.pop()和.append()吗? - Can I use .pop() and .append() on the same item at the same time in Python? 仅当项目符合python中的某些规则时,我才能追加项目 - How can i append a an item only if it meets certain rules in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM