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
json.dumps()returns a string. You can't index a string by a key name as you are doing infinishe = parsed_json['file'].parsed_jsonmakes me think that you've gottenjson.dumpsandjson.loadsconfused.