Return multiple items

2019-08-25 06:30发布

问题:

i am new to python as a matter of fact, this is my first python project. I am using ebaysdk to search for electronics on ebay and i want it to return multiple results because my app is for comparing prices but it returns only one result.

Someone please help me to make the code return multiple results.

Here is my code snippet.

@app.route('/ebay_page_post', methods=['GET', 'POST'])
def ebay_page_post():
    if request.method == 'POST':

        #Get json format of the text sent by Ajax
        search = request.json['search']

        try:
            #ebaysdk code starts here
            api = finding(appid='JohnOkek-hybridse-PRD-5c2330105-9bbb62f2', config_file = None)
            api_request = {'keywords':search, 'outputSelector': 'SellerInfo', 'categoryId': '293'}
            response = api.execute('findItemsAdvanced', api_request)
            soup = BeautifulSoup(response.content, 'lxml')

            totalentries = int(soup.find('totalentries').text)
            items = soup.find_all('item')

            for item in items:
                cat = item.categoryname.string.lower()
                title = item.title.string.lower().strip()
                price = int(round(float(item.currentprice.string)))
                url = item.viewitemurl.string.lower()
                seller = item.sellerusername.text.lower()
                listingtype = item.listingtype.string.lower()
                condition = item.conditiondisplayname.string.lower()

                print ('____________________________________________________________')

                #return json format of the result for Ajax processing
                return jsonify(cat + '|' + title + '|' + str(price) + '|' + url + '|' + seller + '|' + listingtype + '|' + condition)
        except ConnectionError as e:
            return jsonify(e)

回答1:

Based on the code you provided, added the key value pair collection example you could use :

@app.route('/ebay_page_post', methods=['GET', 'POST'])
def ebay_page_post():
    if request.method == 'POST':

        #Get json format of the text sent by Ajax
        search = request.json['search']

        try:

            #ebaysdk code starts here
            api = finding(appid='JohnOkek-hybridse-PRD-5c2330105-9bbb62f2', config_file = None)
        api_request = {'keywords':search, 'outputSelector': 'SellerInfo', 'categoryId': '293'}
        response = api.execute('findItemsAdvanced', api_request)
        soup = BeautifulSoup(response.content, 'lxml')

        totalentries = int(soup.find('totalentries').text)
        items = soup.find_all('item')

        # This will be returned
        itemsFound = {}

        # This index will be incremented 
        # each time an item is added
        index = 0

        for item in items:
            cat = item.categoryname.string.lower()
            title = item.title.string.lower().strip()
            price = int(round(float(item.currentprice.string)))
            url = item.viewitemurl.string.lower()
            seller = item.sellerusername.text.lower()
            listingtype = item.listingtype.string.lower()
            condition = item.conditiondisplayname.string.lower()

            # Adding the item found in the collection
            # index is the key and the item json is the value
            itemsFound[index] = jsonify(cat + '|' + title + '|' + str(price) + '|' + url + '|' + seller + '|' + listingtype + '|' + condition)

            # Increment the index for the next items key
            index++

        for key in itemsFound: 
            print key, ':', itemsFound[key

        # return itemsFound

    except ConnectionError as e:
        return jsonify(e)


回答2:

Once the first item is found, add it to the collection. After your for loop finishes, then return the collection.

Right now you are returning (breaking the iteration) once you have found the first



回答3:

I was able to solve the problem.

Click here to see how i did it

Thanks to every contributor, i am most grateful to you all.