我有一个递归脚本,正在为汽车抓取一个JSON文件。在每个递归级别,它会添加一个新变量,并将其(以及其他值)传递给递归调用,每次都会在信息中越来越详细。我尝试使用locals()
来动态分配变量,但即使在调用之后它仍然是None
(我记得有时候locals()
是只读的)。
我也尝试使用eval()
,它给了我同样的问题(我知道eval并不理想)。理想情况下我想避免使用字典,因为这需要我先用值加载它,这似乎有一些不必要的步骤,但我现在对任何事情持开放态度。
示例:
scraper(manufacturer='Honda')
将刮取模型的JSON文件,设置model='Accord'
然后递归调用
scraper(manufacturer='Honda, model='Accord')
抓取多年的文件,设置为year=2014
并递归调用
scraper(manufacturer='Honda', model='Accord', year='2014')
这是基本案例
def scraper(self, manufacturers, model=None, year=None):
if year:
scrapeurl = '%s&manufacturer=%s&model=%s&year=%s' % (url, manufacturer, model, year)
return someFinalFunction()
elif model:
scrapeurl = '%s&manufacturer=%s&model=%s' % (url, manufacturer, model)
elif manufacturer:
scrapeurl = '%s&manufacturer=%s' % (url, manufacturer)
j = getJSONFromUrl(scrapeurl)
key, values = j.popitems()
for value in values:
locals()[key] = value
return self.scraper(manufacturer, model, year, color)
我很感激有关如何处理这个的任何意见,我知道Python似乎总是有一些聪明的做事方式,而且我总是在学习更多,所以提前谢谢你!我也在这个例子中使用Python3,如果这改变了什么
答案 0 :(得分:2)
locals()['key'] = value
应为locals()[key] = value
更好的是,使用**kwargs
:
def scraper(self, manufacturer, model=None, year=None):
kwargs = dict(manufacturer=manufacturer, model=model, year=year)
if year:
scrapeurl = '%s&manufacturer=%s&model=%s&year=%s' % (url, manufacturer, model, year)
return someFinalFunction()
elif model:
scrapeurl = '%s&manufacturer=%s&model=%s' % (url, manufacturer, model)
elif manufacturer:
scrapeurl = '%s&manufacturer=%s' % (url, manufacturer)
j = getJSONFromUrl(scrapeurl)
key, values = j.popitems()
for value in values:
kwargs[key] = value
return self.scraper(**kwargs)
答案 1 :(得分:1)
目前还不完全清楚你要做什么,但也许这会有所帮助:
def scraper(self, **kwargs):
if kwargs.get('year') is not None:
scrapeurl = '{0}&manufacturer={manufacturer}&model={model}&year={year}'
return someFinalFunction() # not sure why this takes no arguments
elif kwargs.get('model') is not None:
scrapeurl = '{0}&manufacturer={manufacturer}&model={model}'
elif kwargs.get('manufacturer') is not None:
scrapeurl = '{0}&manufacturer={manufacturer}'
else:
raise KeyError
j = getJSONFromUrl(scrapeurl.format(url, **kwargs))
key, values = j.popitems()
for value in values:
kwargs[key] = value
return self.scraper(**kwargs)
这使用Python的内置功能将任意关键字参数作为字典处理,以及更现代的str.format
字符串格式,以动态处理您正在寻找的参数。唯一的区别是你现在需要调用它:
instance.scraper(manufacturer='...')
而不仅仅是
instance.scraper('...')
字符串格式化的一个示例,混合位置和关键字参数:
>>> '{0}&manufacturer={manufacturer}'.format('foo', **{'manufacturer': 'bar'})
'foo&manufacturer=bar'