收集数据python

时间:2016-02-20 16:15:08

标签: python image api flickr

我想从Flickr收集照片的xmls文件,但我的脚本经常停在收集图像数量的地方,然后停止收集哪些时间,我无法修复错误,我必须经常检查以重新收集数据。 任何人都可以突出我的代码中的错误或我应该做什么?

    import flickrapi
    import os
    import sys
    import shutil
    import socket
    import urllib2
    import urllib
    import codecs
    import re
    import socket
    from xml.etree.ElementTree import ElementTree
    import xml.etree.ElementTree as ET
    import httplib
    import numpy as np
    import glob
    import random
    from flickrapi.tokencache import LockingTokenCache


    httplib.HTTPConnection._http_vsn= 10
    httplib.HTTPConnection._http_vsn_str = 'HTTP/1.0'
    api_key = '...........'
    api_secret='........'
    flickr=flickrapi.FlickrAPI(api_key,api_secret)


    print(sys.argv)
    if len(sys.argv) == 3:
      print( "Reading data  " + (sys.argv[1]))
      query_file_name = sys.argv[1] 
      startind = int(sys.argv[1]);
      endind = int(sys.argv[2])
    else:
      print('Number of arguments=%d,program terminated')%(len(sys.argv)-1)
      sys.exit (1)
   new='path to load files'
   head='path to save images'    

   users_list=open(new+'imgs_list.txt','r')
   e=users_list.readlines()
   f=[s.replace('\n','')for s in e]
   print(len(f))
   users_list.close()
   num=0
   i=startind

   while i<=endind:
      try:
          imgid=f[i]
          imgid=imgid.strip('\n')
          num+=1
          photo_info=flickr.photos_getInfo(photo_id=imgid)
          photo_info=ElementTree(photo_info)
          img_info=(head+('/%s.xml'%(imgid)))
          photo_info.write(img_info)
          print(imgid,num,i)

      except KeyboardInterrupt:
          print('keyboard exception while querying for images, exiting\n')
          raise

      except:
          print('error')
          e=sys.exc_info()
          print(e)
          print('missed img info',imgid)
         missed_data=open(new+'/missed_photos.txt','a')
         missed_data.write(imgid+'\n')
         missed_data.close()
         pass

      i+=1 

0 个答案:

没有答案