python - Django- Exception Type: KeyError -
i'm working on application i'm using twitter account url of user logs in application using twitter login scrap total tweet count , number of followers using beautifulsoup
, urllib2
.
views.py
import urllib2 bs4 import beautifulsoup def fillform(request): if request.method == 'post': profile = ['tweets', 'followers'] followers = '' tweets = '' form = evangelizedform(request.post) if form.is_valid(): obj = form.save(commit=true) link = obj.twitter_url dic = {} r = urllib2.urlopen(link) src = r.read() res = beautifulsoup(src) p, d in zip(profile, res.find_all('span', { 'data-is-compact': "true"})): dic[p] = d followers = dic['followers'] tweets = dic['tweets'] obj.twitter_followers = followers obj.total_tweets = tweets obj.save() return index(request) else: form.errors else: #form = evangelizedform() if request.user.is_authenticated(): form = evangelizedform(initial={'first_name': request.user.first_name, 'twitter_url': 'https://www.twitter.com/' + request.user.username, 'last_name': request.user.last_name}) else: form = evangelizedform() context = requestcontext(request, {'request': request, 'user': request.user, 'form':form}) #return render(request, 'rango/fillform.html', {'form': form, 'context_instance':context}) return render_to_response('rango/fillform.html', context_instance=context)
however, error i'm encountering on post request:
traceback: file "c:\python27\lib\site-packages\django\core\handlers\base.py" in get_response 111. response = wrapped_callback(request, *callback_args, **callback_kwargs) file "d:\django\getevangelized\rango\views.py" in fillform 30. followers = dic['followers'] exception type: keyerror @ /rango/fillform/ exception value: 'followers'
i have tried running scraping script locally , keys followers
, tweets
return values stored in dictionary. seems going wrong in view? importing beautifulsoup
, urllib2
modules in django wrong way? or else that's wrong in approach?
Comments
Post a Comment