gpt4 book ai didi

python - 返回 Ajax 的多个项目

转载 作者:太空宇宙 更新时间:2023-11-03 21:37:45 24 4
gpt4 key购买 nike

我有这个 python 代码,用于搜索 eBay 以返回搜索字符串的结果。这些代码正确执行并返回我想要的结果,但仅当我使用 print() 函数时。我正在使用 json 将结果解析到 Flask 页面,使用 print() 仅打印到控制台,因此我使用了 return 但它没有给出所有结果,它只返回一项。

如何让代码返回所有结果并将其显示在我的 Flask 页面上?

我是 python 新手,这是我的第一个项目。我之前问过这个问题,并将代码升级到现在的样子,我一直在互联网上寻找解决方案,但我还没有找到任何解决方案。请有人帮助我编写一个代码,该代码将返回从搜索中获得的所有结果。

Link to my previous question that helped me

后端

@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()

print ('____________________________________________________________')

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

# 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+=1

for key in itemsFound:
return itemsFound[key]

except ConnectionError as e:
return jsonify(e)

Ajax

$(document).ready(function() {
$(".button").click(function() {
var search = $(".search").val().trim();
if (search) {
$.ajax({
type: 'POST',
url: "{{url_for('ebay_page_post')}}",
data: JSON.stringify({ 'search': search }),
contentType: 'application/json;charset=UTF-8',
dataType: "json",
success:function(data) {
if (data.indexOf("|") >= 0) {
var newData = data.split("|");
var category = newData[0];
var title = newData[1];
var price = newData[2]
var url = newData[3];
var seller = newData[4];
var listingType = newData[5];
var condition = newData[6];


$("#returned").html("");
$("#returned").append(returned(category, title, price, url, seller, listingType, condition));
} else {
$("#returned").html("<label>"+data+"</label>");
}
}
});
}
});
});


function returned(category, title, price, url, seller, listingType, condition) {
return '<div class="col-lg-6"> ' +
'<div class="box">'+
'<div class="icon"><i class="ion-ios-heart-outline"></i></div>' +
'<h4 class="title">'+title+'</h4>' +
'<h5>'+category+'</h5>' +
'<small><a href="'+url+'" target="_blank">Go to site</a></small>'+
'<div class="description">' +
'Price: <strong>'+price+'</strong>' +
'<p>Seller: <strong>'+seller+'</strong></p>' +
'<p>'+listingType+'</p>' +
'<p>Condition: '+condition+'</p>' +
'</div>' +
'</div>' +
'</div>'
}

最佳答案

您的 python 代码仅返回 itemsFound 列表中的第一项,因为这就是您告诉它要做的事情:

 for key in itemsFound:
return itemsFound[key]

...为什么不直接返回整个列表呢?就像这样

 return itemsFound

关于python - 返回 Ajax 的多个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53137415/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com