0

为什么 NameError: name 'amazon' is not defined??

如果它不起作用,它就不是问题。

但如果在 amazon_api() 函数中,则会出现错误“未定义名称 'amazon'”。

我怎样才能使用没有错误的 amazon_api() 函数?

import bottlenose
from bs4 import BeautifulSoup


def item_search(keywords, search_index="Toys", item_page= 1):
    response = amazon.ItemSearch(SearchIndex=search_index, Keywords=keywords, ItemPage=item_page, ResponseGroup="Large")
    soup = BeautifulSoup(response, 'lxml')
    return soup.find('item')

def item_Lookup(item_id):
    response = amazon.ItemLookup(ItemId= item_id,  ResponseGroup="Images")
    soup = BeautifulSoup(response, 'lxml')
    return soup.find('largeimage')

def amazon_api():
    api_id = 'your api_id'
    api_key = 'your api_key'
    tag_id = 'your tag_id'

    amazon = bottlenose.Amazon(api_id,api_key,tag_id)    
    item = item_search('nike', search_index="All", item_page= 1)
    asin = item.find('asin').text

    print(asin)
    img = item_Lookup(asin)
    print(img)

amazon_api()

回溯(最后一次调用):文件“C:\Python34\bottlenose.py”,第 28 行,在 amazon_api() 文件“C:\Python34\bottlenose.py”,第 21 行,在 amazon_api item = item_search('nike ', search_index="All", item_page= 1) 文件“C:\Python34\bottlenose.py”,第 6 行,在 item_search response = amazon.ItemSearch(SearchIndex=search_index, Keywords=keywords, ItemPage=item_page, ResponseGroup="大”)名称错误:名称“亚马逊”未定义

4

0 回答 0