0

I'm using google search API to extract information from the google search and displaying it as well as saving it in the console

I'm having error, as the code is not going into the for loop

Here is the code:

import csv
from xgoogle.search import GoogleSearch,SearchError
try:
  print("In Try ")
  gs = GoogleSearch(' Mercedes Benz')
  gs.results_per_page=10
  results=gs.get_results()
  print" results done "
  for res in results: 
    print(" In  For Loop")
    urll=res.url.encode("utf-8") 
    titles= res.title.encode("utf-8")
    descs= res.desc.encode("utf-8")
    print urll
    print titles
    print descs
   # c.writerow[(urll,descs,titles)[]

    try:
        with open('sample.csv', 'wb') as csvfile:
            we = csv.writer(csvfile, delimiter=',')
            we.writerow[(urll,titles,descs)]
            print " Finished"

    except Exception:
        print" Not Finished"          
   except  SearchError, e:
    print " Search Failed : %s" %e

Here is the output from the console

       >>> runfile('D:/Search Engine/SearchEngine.py', wdir='D:/Search 
      Engine')
      In Try 
    results done 
  >>> 
  • what's the result of `gs.get_results()`? – Oluwafemi Sule Jun 02 '17 at 08:01
  • @OluwafemiSule get_results() method returns a SearchResult object. It has three attributes -- "title", "desc", and "url". They are Unicode strings, so a proper encoding is required before outputting them. – Vipul.Johri Jun 02 '17 at 08:38

0 Answers0