我无法在我的python代码

时间:2016-07-18 22:00:25

标签: python json django dictionary

我编写了一个函数,它从一个站点脚本元素中获取代码,剥离字符,使用字符串替换将双引号添加到键,然后返回的是我认为的字典。但我无法从中汲取价值。我试图通过

来访问值
results['file']

那并没有奏效。我收到一条错误消息,上面写着

it expected a number and instead got a string.

然后我尝试了

results[0]

并且返回的所有内容都是

{

我也试过

results.file

那并没有奏效。因此,当谷歌搜索和研究python书籍时,我看到在这样的字典上使用了值函数

dict.values()

但是当我尝试它时我得到了

'str' object has no attribute 'values'

继承我的功能代码

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, "")


            #Allows for multiple string replacement
            rdict = {
                'file': '"file"',
                'image': '"image"',
                'primary': '"primary"',
                'stretching': '"stretching"',
                'width': '"width"',
                'aspectratio': '"aspectratio"',
                'autostart': '"autostart"',
                'logo': '"logo"',
                'position': '"position"',
                # 'link': '"link"',
                'sharing': '"sharing"',
            }

            robj = re.compile('|'.join(rdict.keys()))
            result = robj.sub(lambda m: rdict[m.group(0)], newstring_two)
            parsed_json = json.loads(json.dumps(result))

            soc = parsed_json.replace('link', ' "link" ', 2)

            return soc

这是它返回的内容

{"file":"http://www.youtube.com/watch?v=slCSLeZueI4",
  "image":"http://i1.ytimg.com/vi/slCSLeZueI4/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/3178/la-nina-y-el-lobo-video-oficial/","sites":
       ["facebook","twitter","linkedin","pinterest","tumblr","googleplus","reddit"]}}

我在json验证器中对它进行了测试,它有像json这样的双引号,它用大括号括起来。使这项工作正确的语法是什么。这也是一个django应用程序。我认为这只是为了完整,以防这有所不同

编辑: 我的views.py

        pan = panties()
        context = {
            'pan': pan,
        }

并在我的模板中

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

1 个答案:

答案 0 :(得分:0)

消息

'str' object has no attribute 'values'

告诉你&#34;结果&#34;是一个字符串。您可以使用python-json将字符串解码为python对象:

somevariable = json.loads(results)