Having a hard time to 'get' bing api search results

Yves S. Garret yoursurrogategod at gmail.com
Fri Jun 14 14:19:57 EDT 2013


On Fri, Jun 14, 2013 at 2:04 PM, Kevin LaTona <kl at studiosola.com> wrote:

>
>
> Sounds like you are missing a closing } json bracket
>
>
> Try a different json validator site to prove your json doc is working.
>
>
> If it's the same doc you sent me last night it worked for me
>
>  so maybe it got messed up moving it around?????
>
>
>
>
>
> On Jun 14, 2013, at 10:53 AM, "Yves S. Garret" <yoursurrogategod at gmail.com>
> wrote:
>
> Hi Kevin, still more of the same:
> http://bin.cakephp.org/view/1358843680
>
> The file _is_ there.  I did check the JSON file, in this site:
> - http://paulisageek.com/json_validator/
>
> I got an error stating this:
> expected property name or '}'
>
> And I don't know what it means, especially if my JSON looks like this:
> http://bin.cakephp.org/view/33141714
>
>
> On Fri, Jun 14, 2013 at 12:40 PM, Kevin LaTona <lists at studiosola.com>wrote:
>
>>
>>  import json
>> >>> from pprint import pprint
>> >>> path = '/home/azureuser/temp.json'
>> >>> with open(data) as data_file:
>> ...   data = json.load(data_file)
>>
>>
>>
>> change this to
>>
>>
>> with open(path) as data_file:
>>
>>
>>
>> you need to tell the open command what file path to read.
>>
>>
>>
>>
>>
>> On Jun 14, 2013, at 9:28 AM, "Yves S. Garret" <yoursurrogategod at gmail.com>
>> wrote:
>>
>> > Hi Kevin,
>> >
>> > This is the file.
>> > http://bin.cakephp.org/view/1893672423
>> >
>> >
>> > On Fri, Jun 14, 2013 at 12:20 PM, Kevin LaTona <kl at studiosola.com>
>> wrote:
>> >
>> >
>> >
>> >>
>> >> Traceback
>> >> (most recent call last):
>> >>
>> >>   File
>> >> "<stdin>", line 1, in <module>
>> >>
>> >> NameError: name 'data' is not defined
>> >
>> >
>> >
>> > It looks like it saying it did not open and read your json document.
>> >
>> > Are you 100% sure that is sitting in your home directory?
>> >
>> > Trying opening it with nano or some other text editor at that path
>> >
>> > or try
>> >
>> > cat /home/azureuser/temp.json
>> >
>> > and see what you get
>> >
>> >
>> >
>> > Your are close…… so close.
>> >
>> >
>> > On Jun 14, 2013, at 9:03 AM, Yves S. Garret <yoursurrogategod at gmail.com>
>> wrote:
>> >
>> >> Hi Kevin,
>> >>
>> >> I'm not sure what I'm doing wrong... this is what I did in the shell
>> and the error that I got:
>> >> http://bin.cakephp.org/view/49070824
>> >>
>> >> I'll keep hacking away at this problem...
>> >>
>> >>
>> >> On Thu, Jun 13, 2013 at 11:26 PM, Kevin LaTona <lists at studiosola.com>
>> wrote:
>> >>
>> >> both are working for me.
>> >>
>> >>
>> >>
>> >>
>> >> import json
>> >> from pprint import pprint
>> >>
>> >> path = '/Users/yves/ms.json'
>> >>
>> >>
>> >> with open(path) as data_file:
>> >>     data = json.load(data_file)
>> >>
>> >>
>> >>
>> >> this will allow you to walk through
>> >>
>> >> [ list ]  requires a number else should be mostly just names pulled
>> out of the jSon doc
>> >>
>> >> Xbox One is a state-of-the art gaming console, a new generation TV and
>> movie system and a whole lot more. Gone are the days of switching inputs on
>> your TV to play a game or watch a movie.
>> >>
>> >>
>> >> *************
>> >>
>> >>
>> >>
>> >>
>> >> print data['d']['results'][0]['Description']
>> >>
>> >>
>> >>
>> >>
>> >> pretty print
>> >>
>> >>
>> >>
>> >> pprint(data)
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >> {u'd': {u'__next': u"
>> https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/Web?Query='xbox%20one'&$skip=2&$top=2
>> ",
>> >>         u'results': [{u'Description': u'Xbox One is a state-of-the art
>> gaming console, a new generation TV and movie system and a whole lot more.
>> Gone are the days of switching inputs on your TV to play a game or watch a
>> movie.',
>> >>                       u'DisplayUrl': u'
>> www.xbox.com/en-US/xboxone/meet-xbox-one',
>> >>                       u'ID': u'39bc6e59-8323-4275-a992-cb434179e29f',
>> >>                       u'Title': u'Introducing Xbox One',
>> >>                       u'Url': u'
>> http://www.xbox.com/en-US/xboxone/meet-xbox-one',
>> >>                       u'__metadata': {u'type': u'WebResult',
>> >>                                       u'uri': u"
>> https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/Web?Query='xboxone'&$skip=0&$top=1"}},
>> >>                      {u'Description': u'Entertainment is More Amazing
>> with Xbox. Official site of Xbox 360 + Kinect. Xbox LIVE, social, games,
>> video, music, support',
>> >>                       u'DisplayUrl': u'www.xbox.com/en-US',
>> >>                       u'ID': u'06a3d093-ddb4-418b-b34e-a81a0203d3fb',
>> >>                       u'Title': u'Xbox 360 - Official Site - Xbox.com
>> ',
>> >>                       u'Url': u'http://www.xbox.com/en-US/',
>> >>                       u'__metadata': {u'type': u'WebResult',
>> >>                                       u'uri': u"
>> https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/Web?Query='xboxone'&$skip=1&$top=1"}}]}}
>> >> [Finished in 0.1s]
>> >>
>> >>
>> >>
>> >>
>> >> On Jun 13, 2013, at 8:12 PM, "Yves S. Garret" <
>> yoursurrogategod at gmail.com> wrote:
>> >>
>> >>> I don't think that it's the print method.  This is me experimenting
>> with
>> >>> various options:
>> >>> http://bin.cakephp.org/view/111535571
>> >>>
>> >>> This is the JSON file that I was using:
>> >>> http://bin.cakephp.org/view/1423034899
>> >>>
>> >>> I'd figure that a JSON file generated by MS would 99.99% of the time
>> >>> be a good file...
>> >>>
>> >>>
>> >>> On Thu, Jun 13, 2013 at 11:07 PM, Kevin LaTona <lists at studiosola.com>
>> wrote:
>> >>>
>> >>> Then with statement is a good thing.
>> >>>
>> >>> looks like pprint is choking you.
>> >>>
>> >>> To be honest I have not used pprint much
>> >>>
>> >>> and just grab jSON doc data as needed direct from doc.
>> >>>
>> >>> print data['some_name']   ( fill in json doc key name )  should get
>> it.
>> >>>
>> >>>
>> >>> My guess is you will need to just look at json doc and figure out the
>> pattern you need.
>> >>>
>> >>>
>> >>>
>> >>> Have you done much work direct with JSON docs?
>> >>>
>> >>> Stackoverflow will have tons of example to help you along those lines.
>> >>>
>> >>>
>> >>>
>> >>> On Jun 13, 2013, at 7:53 PM, Yves S. Garret <
>> yoursurrogategod at gmail.com> wrote:
>> >>>
>> >>>> I should have mentioned it earlier, but I'm using Python 2.7.5.
>> >>>>
>> >>>>
>> >>>> On Thu, Jun 13, 2013 at 9:43 PM, Kevin LaTona <lists at studiosola.com>
>> wrote:
>> >>>>
>> >>>>
>> >>>> if your json doc is valid json
>> >>>>
>> >>>> try using this code as an example if you are using 2.7 +
>> >>>>
>> >>>> The With statement is very cool as when you leave it will close the
>> JSon doc for you.
>> >>>>
>> >>>>
>> >>>>
>> >>>> http://stackoverflow.com/a/13633860
>> >>>>
>> >>>> import
>> >>>>  json
>> >>>>
>> >>>> from pprint import
>> >>>>  pprint
>> >>>>
>> >>>>
>> >>>> with open('data.json') as data_file:
>> >>>>
>> >>>>     data
>> >>>> = json.load(data_file)
>> >>>>
>> >>>> pprint
>> >>>> (data)
>> >>>>
>> >>>>
>> >>>>
>> >>>>
>> >>>>
>> >>>>
>> >>>> On Jun 13, 2013, at 6:00 PM, Yves S. Garret <
>> yoursurrogategod at gmail.com> wrote:
>> >>>>
>> >>>>> Thanks again Kevin.  I'm deviating from the original thread,
>> >>>>> but I've got another issue.  When I try to load the json file
>> >>>>> and then parse it, this is the error that I get:
>> >>>>> http://bin.cakephp.org/view/1329549559
>> >>>>>
>> >>>>>
>> >>>>> On Thu, Jun 13, 2013 at 6:57 PM, Kevin LaTona <lists at studiosola.com>
>> wrote:
>> >>>>>
>> >>>>>
>> >>>>> Your welcome.
>> >>>>>
>> >>>>>
>> >>>>> To be honest I am not 100% on the differences between.
>> >>>>>
>> >>>>> I could be off, but I recall urllib2 was a more refined version of
>> urllib.
>> >>>>>
>> >>>>> Yet it seems like urllib works better for me, when I need to do a
>> simple call like this.
>> >>>>>
>> >>>>>
>> >>>>> -Kevin
>> >>>>>
>> >>>>>
>> >>>>>
>> >>>>> On Jun 13, 2013, at 3:50 PM, "Yves S. Garret" <
>> yoursurrogategod at gmail.com> wrote:
>> >>>>>
>> >>>>>> That works beautifully!  Thank you!
>> >>>>>>
>> >>>>>> I do have one question, what are urllib and urllib2 then?  I
>> figured that
>> >>>>>> urllib2 is a newer version of the previous library (and one that I
>> should
>> >>>>>> be using).  Am I missing something?
>> >>>>>>
>> >>>>>>
>> >>>>>> On Thu, Jun 13, 2013 at 6:45 PM, Kevin LaTona <
>> lists at studiosola.com> wrote:
>> >>>>>>
>> >>>>>> I did a quick test with url lib instead of urllib2 and got closer.
>> >>>>>>
>> >>>>>> Problem right now is without ID code I can't check any further.
>> >>>>>>
>> >>>>>> But it does look promising at this point.
>> >>>>>>
>> >>>>>>
>> >>>>>> If all else fails
>> http://docs.python.org/2/library/urllib.html#examples
>> >>>>>>
>> >>>>>>
>> >>>>>> import urllib
>> >>>>>>
>> >>>>>> f = urllib.urlopen('
>> https://user:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA@api.datamarket.azure.com/Bing/SearchWeb/Web?Query=%27xbox%20one%27&$top=50&$format=JSON
>> ')
>> >>>>>>
>> >>>>>> print f.read()
>> >>>>>>
>> >>>>>>
>> >>>>>>
>> >>>>>> IOError: ('http error', 401, 'The authorization type you provided
>> is not supported.  Only Basic and OAuth are supported',
>> >>>>>>
>> >>>>>>
>> >>>>>>
>> >>>>>>
>> >>>>>>
>> >>>>>> On Jun 13, 2013, at 2:31 PM, Yves S. Garret <
>> yoursurrogategod at gmail.com> wrote:
>> >>>>>>
>> >>>>>>> This is the format that I've been following:
>> >>>>>>>
>> http://gavinmhackeling.com/blog/2012/05/using-the-bing-search-api-in-python/
>> >>>>>>>
>> >>>>>>> If I execute the specified query from a browser, the JSON file
>> >>>>>>> shows up without a problem.  Now, I'd like to do that
>> programmatically.
>> >>>>>>>
>> >>>>>>>
>> >>>>>>> On Thu, Jun 13, 2013 at 4:57 PM, Yves S. Garret <
>> yoursurrogategod at gmail.com> wrote:
>> >>>>>>> Hello all,
>> >>>>>>>
>> >>>>>>> This is my dilemma, I'm trying to get the generated JSON file
>> using the bing api
>> >>>>>>> search.
>> >>>>>>>
>> >>>>>>> This is the code that I'm executing from inside the shell:
>> >>>>>>> http://bin.cakephp.org/view/460660617
>> >>>>>>>
>> >>>>>>> The port doesn't matter to me.  Thoughts?
>> >>>>>>>
>> >>>>>>> --
>> >>>>>>> http://mail.python.org/mailman/listinfo/python-list
>> >>>>>>
>> >>>>>>
>> >>>>>
>> >>>>>
>> >>>>
>> >>>>
>> >>>
>> >>>
>> >>
>> >>
>> >
>> >
>>
>>
>
>
Ooops, I think we've left the mailing list off of this one.

This is the error that I'm getting right now.
>>> import json
>>> from pprint import pprint
>>> path = '/home/azureuser/temp.json'
>>> with open(path) as data_file:
...   data = json.load(data_file)
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/usr/local/lib/python2.7/json/__init__.py", line 290, in load
    **kw)
  File "/usr/local/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python2.7/json/decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/lib/python2.7/json/decoder.py", line 381, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 2 column 1 (char 2)

Thanks for your inputs Andreas.

Also, this is my JSON file, I'll post again.  Since it's going to be
messy, I'll include a link as well (users can decide what they prefer
to read).
http://bin.cakephp.org/view/1050217914

{
  "d": {
    "results": [
      {
        "__metadata": {
          "uri": "
https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/Web?Query='xboxone'&$skip=0&$top=1",
          "type": "WebResult"
        },
        "ID": "39bc6e59-8323-4275-a992-cb434179e29f",
        "Title": "Introducing Xbox One",
        "Description": "Xbox One is a state-of-the art gaming console, a
new generation TV and movie system and a whole lot more. Gone are the days
of switching inputs on your TV to play a game or watch a movie.",
        "DisplayUrl": "www.xbox.com/en-US/xboxone/meet-xbox-one",
        "Url": "http://www.xbox.com/en-US/xboxone/meet-xbox-one"
      },
      {
        "__metadata": {
          "uri": "
https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/Web?Query='xboxone'&$skip=1&$top=1",
          "type": "WebResult"
        },
        "ID": "06a3d093-ddb4-418b-b34e-a81a0203d3fb",
        "Title": "Xbox 360 - Official Site - Xbox.com",
        "Description": "Entertainment is More Amazing with Xbox. Official
site of Xbox 360 + Kinect. Xbox LIVE, social, games, video, music, support",
        "DisplayUrl": "www.xbox.com/en-US",
        "Url": "http://www.xbox.com/en-US/"
      }
    ],
    "__next": "
https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/Web?Query='xbox%20one'&$skip=2&$top=2
"
  }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130614/ed612e7c/attachment.html>


More information about the Python-list mailing list