如何访问从结果中返回的字典?

时间:2016-07-18 00:51:14

标签: python django dictionary

我使用beutifulsoup创建了一个函数,它返回看起来像结果字典的内容。但如果我试试

results['file']

results[0]

它没有返回我想要的结果。我想要文件和图像。继承我的代码

    def panties():
        pan_url = 'http://www.panvideos.com'
        html = requests.get(pan_url, headers=headers)
        soup = BeautifulSoup(html.text, 'html5lib')
        video_row = soup.find_all('div', {'class': 'video'})

        def youtube_link(url):
            youtube_page = requests.get(url, headers=headers)
            soupdata = BeautifulSoup(youtube_page.text, 'html5lib')
            video_row = soupdata.find('div', {'class': 'video-player'})
            entries = [{'text': str(div),
                        } for div in video_row][3]['text']

            oldstring = str(entries)
            removed = '<script type="text/javascript">jwplayer("video-setup").setup('
            newstring = oldstring.replace(removed, "")
            removed_two = ');</script>'
            newstring_two = newstring.replace(removed_two, "")

            return newstring_two

        entries = [{'text': div.h4.text,
                    'href': div.a.get('href'),
                    'tube': youtube_link(div.a.get('href')),
                    } for div in video_row][:1]

        return entries

并且继承了它的回报

{file:"http://www.youtube.com/watch?v=jucBuAzuZ0E",image:"http://i1.ytimg.com/vi/jucBuAzuZ0E/maxresdefault.jpg",primary:"html5",stretching:"fill","controlbar":"bottom",width:"100%",aspectratio:"16:9",autostart:"true",logo:{file:"http://www.panvideos.com/uploads/bien-png578aab16676e1.png",position:"bottom-right",link:"http://www.panvideos.com/"},sharing:{link:"http://www.panvideos.com/video/3020/alejandro-sanz-deja-que-te-bese-ft-marc-anthony-official-video-/","sites":["facebook","twitter","linkedin","pinterest","tumblr","googleplus","reddit"]}}

我真正想要的只是

file:"http://www.youtube.com/watchv=jucBuAzuZ0E"
image:"http://i1.ytimg.com/vi/jucBuAzuZ0E/maxresdefault.jpg"

我该怎么抓住这个?我正在使用django,我试着这样做

{% for p in pan %}
   Title: {{p.text}}<br>
   Href: {{p.href}}<br>
   Tube: {{p.tube['file']}}<hr>
{% endfor %}

但是我遇到了解析错误。如何访问文件和图像?

3 个答案:

答案 0 :(得分:0)

dict = {file:"http://www.youtube.com/watch?v=jucBuAzuZ0E",image:"http://i1.ytimg.com/vi/jucBuAzuZ0E/maxresdefault.jpg",primary:"html5",stretching:"fill","controlbar":"bottom",width:"100%",aspectratio:"16:9",autostart:"true",logo:{file:"http://www.panvideos.com/uploads/bien-png578aab16676e1.png",position:"bottom-right",link:"http://www.panvideos.com/"},sharing:{link:"http://www.panvideos.com/video/3020/alejandro-sanz-deja-que-te-bese-ft-marc-anthony-official-video-/","sites":["facebook","twitter","linkedin","pinterest","tumblr","googleplus","reddit"]}}

你必须编辑你只返回实体的视图文件(我使用了dict变量而不是实体)

return render(request, self.template_name, {'dict': dict})

在html模板中呈现值:

 <p>{{ dict.file }}</p>

希望它会对你有所帮助。

答案 1 :(得分:0)

所以你要回到字典,为了解析字典,只需使用分配给这些值的名称。

实施例: 如果您只想使用文件和图像:

results["file"]
results["image"]

或者您可能需要使用:

entries["file"]
entries["image"]

希望能解决你的问题:)

答案 2 :(得分:-1)

dict.iteritems()方法返回字典(键,值)对上的迭代器。

for k, v in yourdict.iteritems():
    print(k ,v)