0

When I use json.loads I get the following error

string indices must be integers

I was doing some research and saw someone with what I thought was a similar situation. I changed this

json.dumps(newstring_two)

to this

json.loads(json.dumps(newstring_two))

but it still did not work. Here is the function I created

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, "")
            parsed_json = json.dumps(newstring_two)
            finishe = parsed_json['file']

            return finishe

        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

but it's not working. What's the issue?

EDIT: my full trace back

        Traceback (most recent call last):
      File "/Users/ray/Desktop/oku/practice/lib/python3.5/site-packages/django/core/handlers/base.py", line 149, in get_response
        response = self.process_exception_by_middleware(e, request)
      File "/Users/ray/Desktop/oku/practice/lib/python3.5/site-packages/django/core/handlers/base.py", line 147, in get_response
        response = wrapped_callback(request, *callback_args, **callback_kwargs)
      File "/Users/ray/Desktop/oku/super_nu/src/blog/views.py", line 164, in index
        pan = panties()
      File "/Users/ray/Desktop/oku/super_nu/src/blog/my_scraps.py", line 166, in panties
        } for div in video_row][:1]
      File "/Users/ray/Desktop/oku/super_nu/src/blog/my_scraps.py", line 166, in <listcomp>
        } for div in video_row][:1]
      File "/Users/ray/Desktop/oku/super_nu/src/blog/my_scraps.py", line 159, in youtube_link
        finishe = parsed_json['file']
    TypeError: string indices must be integers
8
  • Please add the full error traceback to your question! Commented Jul 18, 2016 at 4:25
  • 2
    json.dumps() returns a string. You can't index a string by a key name as you are doing in finishe = parsed_json['file']. Commented Jul 18, 2016 at 4:31
  • 2
    Also, the variable name parsed_json makes me think that you've gotten json.dumps and json.loads confused. Commented Jul 18, 2016 at 4:32
  • @JohnGordon Looks like an answer Commented Jul 18, 2016 at 4:35
  • @JohnGordon the variable parsed_json initially used json.loads, but I was getting an error that was talking about double quotes. I saw in hitchhikers guide to python that json.dumps changes the single quotes to double quotes. Json is strict about the quotes Commented Jul 18, 2016 at 4:38

2 Answers 2

1

'newstring_two' is string, so parsed_json = json.dumps(newstring_two) should be replaced parsed_json = json.loads(newstring_two). see JSON encoder and decoder

Sign up to request clarification or add additional context in comments.

2 Comments

when I do what you mentioned and do this parsed_json['file'] I get the following error message "Expecting property name enclosed in double quotes: line 1 column 2 (char 1)"
newstring_two is not standard json string. If you just get file field,you can use re. for example tube_file = re.search('file:"(.*?)",', new_string_two) print(tube_file.group(1))
0

You should use a integer instead of 'file' statement at that line:

finishe = parsed_json['file']

So, it can be so:

finishe = parsed_json[0]

The 0 number at example can be 1,2,3,4 ..... You must find out which is the true number.

2 Comments

Thanks for responding but that doesn't work. Doing that only returns a single character. The output of doing that would be "{"
try that: str = "" for i in parsed_json: str+=i print str what do you see when you do it ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.