I have this python code that searches ebay to return results for a serach string. The codes executes properly and returns the result as i want but only when i use the print() function. I am parsing the result using json to a flask page, using print() only prints to the console so i used return but it doesn’t give all the results, it returns only one item.
How do i make the code return all the results and display it on my flask page?
I am new to python and this is my first project. I have asked this question before and got the code upgraded to what it is now and i have been scouring the internet for solution but i haven’t found any yet. Someone please help me with a code that will return all the results gotten from the search.
Link to my previous question that helped me
Backend
@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>'
}
2
Answers
I was able to solve the problem by modifying my code to this:
Then i modified my ajax call to this:
To see the result check it out here
Thanks to every one who helped.
Your python code is only return the first item from your
itemsFound
list because that is what you are telling it to do:… why not just return the whole list? Like so