开发者

Error while trying to parse a website url using python . how to debug it?

开发者 https://www.devze.com 2023-02-01 01:50 出处:网络
#!/usr/bin/python import json import urllib from BeautifulSoup import BeautifulSoup from BeautifulSoup import BeautifulStoneSoup
#!/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开发者_Python百科(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 ?


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消