比较来自世界各地的卖家的域名和 IT 服务价格

如何修复以下错误 AttributeError: 'dict' 对象没有属性 "文本"

我一直从事编程数月。 目前,我学习如何自动化项目中的某些事物。 我的目标是清除文本, src 和 href 并在我的网站数据库中保存数据,但是当我尝试时,我会收到此错误


AttributeError: 'dict' object has no attribute 'text'


但它是如此。 这是我的代码。 我创建了一个函数


def get_world_too//:
url = 'http://www.example.com'
html = requests.get/url, headers=headers/
soup = BeautifulSoup/html.text, 'html5lib'/

titles = soup.find_all/'section', 'box'/[:9]
entries = [{'href': box.a.get/'href'/,
'src': box.img.get/'src'/,
'text': box.strong.a.text,
'url': url
} for box in titles]
return entries


然后我做到了


def noindex/request/:
world = get_world_too//

for entry in world:
post = Post//
post.title = entry.text
post.image_url = entry.src
# post.url = entry.url
# post.link = entry.href
# post.description = entry.description
#
# d = datetime.datetime/*/entry.published_parsed[0:6]//
# date_string = d.strftime/'%Y-%m-%d %H:%M:%S'/
#
# post.publication_date = date_string
post.save//

template = "blog/post/noindex.html"
context = {

}
return render/request, template, context/


文本属性不是我的函数吗? 然后,如果我试图发表文本,他会告诉我


AttributeError: 'dict' object has no attribute 'src'


如何修复它,以便您需要存储在我的数据库中没有任何错误的数据? 我用 django, 如果重要的话。
已邀请:

帅驴

赞同来自:

您必须访问那样的字典的键:


entry['text']
entry['src']


只是不喜欢那样


entry.text
entry.src

君笑尘

赞同来自:

在 python 您无法使用语法访问字典元素 dict.key ,
如果记录是字典,则可以使用


entry['key1']



entry.get/'key'/

要回复问题请先登录注册