在Google Scraping中使用Python打印结果时出错

时间:2017-06-02 07:26:31

标签: python python-2.7 web-scraping xgoogle

我使用谷歌搜索API从谷歌搜索中提取信息并显示它,并将其保存在控制台中

我有错误,因为代码没有进入for循环

以下是代码:

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

以下是控制台的输出

       >>> runfile('D:/Search Engine/SearchEngine.py', wdir='D:/Search 
      Engine')
      In Try 
    results done 
  >>> 

0 个答案:

没有答案