首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >返回多个项目

返回多个项目
EN

Stack Overflow用户
提问于 2018-11-02 00:42:59
回答 3查看 224关注 0票数 0

我是python的新手,事实上,这是我的第一个python项目。我正在使用ebaysdk在ebay上搜索电子产品,我希望它返回多个结果,因为我的应用程序是用来比较价格的,但它只返回一个结果。

谁能帮我让代码返回多个结果。

这是我的代码片段。

代码语言:javascript
复制
@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)
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-11-22 10:54:23

我能够解决这个问题。

Click here to see how i did it

感谢每一位贡献者,我非常感谢你们。

票数 0
EN

Stack Overflow用户

发布于 2018-11-02 01:54:14

根据您提供的代码,添加了您可以使用的键值对集合示例:

代码语言:javascript
复制
@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)
票数 1
EN

Stack Overflow用户

发布于 2018-11-02 00:48:02

找到第一项后,将其添加到集合中。在for循环结束后,返回集合。

现在,一旦你找到了第一个,你就会返回(中断迭代)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53105654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档