Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何修复以下错误AttributeError:';dict';对象没有属性';文本';_Python_Database_Screen Scraping - Fatal编程技术网

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

Python 如何修复以下错误AttributeError:';dict';对象没有属性';文本';,python,database,screen-scraping,Python,Database,Screen Scraping,我已经开始编程几个月了。我目前正在学习如何在项目中自动化某些事情。我的目标是抓取文本、src和href,并将数据存储在我的站点数据库中,但当我尝试时,会出现此错误 AttributeError: 'dict' object has no attribute 'text' 但确实如此。这是我的密码。我创建了一个函数 def get_world_too(): url = 'http://www.example.com' html = requests.get(url

我已经开始编程几个月了。我目前正在学习如何在项目中自动化某些事情。我的目标是抓取文本、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

您必须像这样访问字典键:

entry['text']
entry['src']
不是这样的

entry.text
entry.src

在python中,不能使用语法dict.key访问字典项, 如果条目是字典,您可以使用

  • entry['key1']
  • entry.get('key')

在python中,不能使用语法dict.key访问字典项, 如果条目是字典,您可以使用

  • entry['key1']
  • entry.get('key')