我要回去并完成从我的介绍到comp sci课程的旧课程,并且在其中一个实验室中我应该使用flickrapi模块来刮取flickr以获取用于其余部分的一组图片。实验室。该项目被分配了一些代码来抓取我知道应该工作的flickr,但每当我运行代码时它会抛出一个TypeError。返回错误的函数是:
def getphotos(apicode, query, num_images):
''' Return a list of URLs that have a tag that
matches the query code. '''
# Form the object that will interact with the Flickr website
flickr = flickrapi.FlickrAPI(apicode, format='etree')
# Get each matching photo and store in a list, stopping when we
# reach the target number of images
photos = []
for photo in flickr.walk(tags = query, tag_mode = 'all', safe_search = '0', sort = 'interestingness-desc'):
url = "http://farm" + photo.get('farm') + ".staticflickr.com/" + \
photo.get('server') + "/" + photo.get('id') + "_" + \
photo.get('secret') + ".jpg"
print url
photos.append(url)
if len(photos) >= num_images:
break
return photos
抛出错误的行是flickr = flickrapi.FlickrAPI(apicode, format='etree')
,其中apicode表示flickr给我的apicode键,我不太确定format ='etree'
的作用。当我查看flickrapi模块并进入core.py时,我会进入FlickrAPI类。似乎感兴趣的课程部分如下:
class FlickrAPI(object):
"""Encapsulates Flickr functionality.
Example usage::
flickr = flickrapi.FlickrAPI(api_key)
photos = flickr.photos_search(user_id='73509078@N00', per_page='10')
sets = flickr.photosets_getList(user_id='73509078@N00')
"""
REST_URL = 'https://api.flickr.com/services/rest/'
UPLOAD_URL = 'https://up.flickr.com/services/upload/'
REPLACE_URL = 'https://up.flickr.com/services/replace/'
def __init__(self, api_key, secret, username=None,
token=None, format='etree', store_token=True,
cache=False):
...(followed by logic statements involving the inputs for __init__ and class methods)
当flickr给我一个apicode密钥时,它还会给我一个密钥,我将其存储在.txt文件中,与我正在处理的程序位于同一目录中。
现在很明显,对FlickrAPI类的调用正在传递3个参数,其中2个是apicode键,格式是''etree',但我对第三个是什么有些不确定。该类是以某种方式通过flickr调用密钥,还是 init 的其他输入之一?我如何解决代码给我的类型错误?
答案 0 :(得分:0)
三个必需参数是self
,api_key
和secret
(没有默认值的参数),FlickrAPI文档字符串中给出的示例不起作用。
这里,第一个参数self
是隐式的(它是自己构造的FlickrAPI的实例),你给第二个参数api_key
和apicode
,但是缺少第三个必需参数secret
。 format
是你给出的第三个参数,但它不计入三个必需参数,因为它有一个默认值。
答案 1 :(得分:0)
问题是你没有为secret
传递任何内容。
看一下这个例子
class Test():
def __init__(self, one, two, three=3):
print one, two, three
Test(1, three=3)
Test.__init__
有四个参数,其中一个(three
)具有默认值,因此至少需要self
,one
和two
它。 self
会自动传递,因为它是一个类方法,所以它是第一个。然后你传递one
的内容和three
的内容。您确实传递了三个参数,这是最小值,但您没有为two
传递任何内容,它没有任何默认值。这就是错误的来源。您可以调用具有相同参数数量的Test(1, 2)
,因为这样就会提供没有默认值的所有参数。
在您的代码中,您传递的是api_key
的参数和format
的参数,但没有secret
的参数,它没有默认值。