尝试附加数据时,它们显示错误'dict'对象没有属性'append'



显示了AttributeError: 'dict' object has no attribute 'append'在尝试追加数据时如何处理这些错误我创建了一个循环以便将用户输入的连续值追加到字典中但我得到了这个错误是任何方法来显示解决这些错误这是页面链接https://www.nationalhardwareshow.com/en-us/attend/exhibitor-list.html:

import requests
from bs4 import BeautifulSoup
import json
import pandas as pd
headers = {
'Accept-Language': 'en-GB,en-US;q=0.9,en;q=0.8,pt;q=0.7',
'Connection': 'keep-alive',
'Origin': 'https://www.nationalhardwareshow.com',
'Referer': 'https://www.nationalhardwareshow.com/',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'cross-site',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
'accept': 'application/json',
'content-type': 'application/x-www-form-urlencoded',
'sec-ch-ua': '".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
}
base_url='https://api.reedexpo.com/v1/organisations/'
params = {
'x-algolia-agent': 'Algolia for vanilla JavaScript 3.27.1',
'x-algolia-application-id': 'XD0U5M6Y4R',
'x-algolia-api-key': 'd5cd7d4ec26134ff4a34d736a7f9ad47',
}
data = '{"params":"query=&page=0&facetFilters=&optionalFilters=%5B%5D"}'
resp = requests.post('https://xd0u5m6y4r-3.algolianet.com/1/indexes/event-edition-eve-e6b1ae25-5b9f-457b-83b3-335667332366_en-us/query', params=params, headers=headers, data=data).json()
productlinks=[]
for item in resp['hits']:
url=base_url+item['organisationGuid']+"/exhibiting-organisations?eventEditionId="+item['eventEditionExternalId']
productlinks.append(url)

data=[]   
for link in productlinks:
headers = {"x-clientid": "uhQVcmxLwXAjVtVpTvoerERiZSsNz0om"}
data = requests.get(link, headers=headers).json()
wev={}
for t in data["_embedded"]:
name=t['companyName']
wev['name']=name
email=t['email']
wev['Email']=email
telephone=t["phone"]
wev['Telephone']=telephone
link=t['website']
wev['link']=link
d=t["multilingual"]
for u in d:
k=u["address"]
wev['address']=k
n=u["representedBrands"]
wev['brand']=n

data.append(wev)

df=pd.DataFrame(data)
print(df)

您的问题是您将data初始化为列表并重新分配给dict数据结构。为不同的目的使用不同的变量:

store = []
for link in productlinks:
data = requests.get("...").json()
# process data ...
store.append(data)
df = pd.DataFrame(store)

一般来说,为了向python字典中添加新元素,您应该使用:

old_dict  = {}
old_dict["key_name"] = 42

"key_name"也可以是其他类型。在"追加"之前必须有一个键。

相关内容

  • 没有找到相关文章