【问题标题】:How can I access the dictionary returned from my results?如何访问从结果返回的字典?
【发布时间】:2016-07-18 00:51:14
【问题描述】:

我使用 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 %}

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

【问题讨论】:

    标签: python django dictionary


    【解决方案1】:
    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>
    

    希望对你有帮助。

    【讨论】:

    • 感谢@aroy 的回复,但这会在我的代码中出现。并且字典必须是每个视频拉取的唯一的。看来你想让我把它放在我的观点中
    • 如果您将 pan 指定为字典,您需要做的是 {% for key,value in pan.items %} 在其中提取文件只需使用 {{ pan.items.key }}
    • gun.io/blog/…得到这个想法希望它能解决你的问题
    【解决方案2】:

    所以你得到了一个字典,为了解析字典,只需使用分配给值的名称。

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

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

    或者您可能需要使用:

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

    希望能解决你的问题:)

    【讨论】:

    • 已经尝试过并收到以下消息“字符串索引必须是整数”
    【解决方案3】:

    dict.iteritems() 方法返回字典的 (key, value) 对的迭代器。

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

    【讨论】:

      猜你喜欢
      • 2016-11-19
      • 2021-02-21
      • 2021-01-10
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      • 2017-11-23
      相关资源
      最近更新 更多