amoldabade7 / pybing

Automatically exported from code.google.com/p/pybing
Other
0 stars 0 forks source link

json loads error #9

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
This is the example:
from pybing import Bing
bing = Bing('APPID')
resp = bing.search_web('python')
print resp['SearchResponse']['Web']['Total']

Here is the output :
Traceback (most recent call last):
  File "test1.py", line 4, in <module>
    resp = bing.search_web('python')
  File "/home/zz/Desktop/bruteforce/pybing/bing.py", line 48, in search_web
    return self.search(query, source_type=constants.WEB_SOURCE_TYPE)
  File "/home/zz/Desktop/bruteforce/pybing/bing.py", line 45, in search
    return json.loads(contents)
  File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer

I am using python2.7 and the latest pybing

diff --git a/pybing/bing.py b/pybing/bing.py
index 3063e6b..4fd68bc 100644
--- a/pybing/bing.py
+++ b/pybing/bing.py
@@ -40,7 +40,7 @@ class Bing(object):

         query_string = urllib.urlencode(kwargs)
         contents = urllib2.urlopen(constants.JSON_ENDPOINT + '?' + query_string
-        return json.loads(contents)
+        return json.loads(contents.read())

     def search_web(self, query):
         return self.search(query, source_type=constants.WEB_SOURCE_TYPE)

Original issue reported on code.google.com by zzs...@gmail.com on 17 Feb 2012 at 2:21

GoogleCodeExporter commented 8 years ago
I am having this problem as well, but worked around it by using the Query API 
wrapper (example on homepage)

Original comment by jonathan...@gmail.com on 23 Feb 2012 at 11:55

GoogleCodeExporter commented 8 years ago
I am also having this issue using 2.6.5.

Original comment by brandon....@gmail.com on 1 Mar 2012 at 12:07

GoogleCodeExporter commented 8 years ago
Just posted a change according to the diff. Let me know if that fixes it.

Original comment by jgeewax on 1 Mar 2012 at 12:13

GoogleCodeExporter commented 8 years ago
@jgeewax, I had the same problem and your change worked, thanks :)

To save any future users some time:
-open pybing/bing.py
-change return json.loads(contents) to return json.loads(contents.read())

For some reason pulling the trunk didn't include this update...
svn checkout http://pybing.googlecode.com/svn/trunk/ 

Original comment by colorado...@gmail.com on 22 Mar 2012 at 3:59