0
#!/usr/bin/python
import json
import urllib
from BeautifulSoup import BeautifulSoup
from BeautifulSoup import BeautifulStoneSoup
import BeautifulSoup  


def showsome(searchfor):
  query = urllib.urlencode({'q': searchfor})
  url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query
  search_response = urllib.urlopen(url)
  search_results = search_response.read()
  results = json.loads(search_results)
  data = results['responseData']
  print 'Total results: %s' % data['cursor']['estimatedResultCount']
  hits = data['results']
  print 'Top %d hits:' % len(hits)
  for h in hits: 
    print ' ', h['url']
    resp = urllib.urlopen(h['url'])
        res = resp.read()

    soup = BeautifulSoup(res)
        print soup.prettify()
  print 'For more results, see %s' % data['cursor']['moreResultsUrl']

showsome('sachin')

What is the wrong in this code ?

Note all the 4 links that I am getting out of the search , I am feeding it back to extract the contents out of it , and then use BeautifulSoup to parse it . How should I go about it ?

3
  • 1
    I don't know whats wrong. What's wrong? Doesn't it work? How about explaining what the problem is? :-) Commented Dec 21, 2010 at 7:16
  • I want to parse the content of the of url which I am calling , and the reponse that is stored in the variable "resp" . So the question is once I have the response how Do I parse the contents of it ? Commented Dec 21, 2010 at 7:18
  • You said "Error while trying to...". What was the exact error? Commented Dec 21, 2010 at 7:20

3 Answers 3

1

What is the wrong in this code ?

Your indentation is all wonky in the for loop, and this line:

import BeautifulSoup  

should be deleted, as it masks this earlier import:

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

Comments

0

It appears you're trying to access the Google ajax API in Python. You might consider using the Xgoogle library available on Github: https://github.com/pkrumins/xgoogle

Comments

0

You can debug python code in a proper environment that lets you examine what is going on with the variables, stacks and all using the eclipse ide with a module name pydev, try it to see what is going on inside.

Comments

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.