CA
Crawlee & Apify•3y ago
xenial-black

Display multiple output

My current actor only outputs one result from a list of inputs. I'm not sure what else I need to add to my code for all results to get displayed. Here's what I have for my output:
book.append({'title': get_title(doc),
'price': get_price(doc),
'publisher': get_pub(doc),
'copyright': get_copy(doc),
'asin': get_asin(doc),
'isbn': get_isbn(doc),
'description': get_desc(doc),
'image': get_image(doc)})

default_dataset_client = client.dataset(os.environ['APIFY_DEFAULT_DATASET_ID'])
default_dataset_client.push_items(book)
print(f'Results have been saved to the dataset with ID {os.environ["APIFY_DEFAULT_DATASET_ID"]}')
book.append({'title': get_title(doc),
'price': get_price(doc),
'publisher': get_pub(doc),
'copyright': get_copy(doc),
'asin': get_asin(doc),
'isbn': get_isbn(doc),
'description': get_desc(doc),
'image': get_image(doc)})

default_dataset_client = client.dataset(os.environ['APIFY_DEFAULT_DATASET_ID'])
default_dataset_client.push_items(book)
print(f'Results have been saved to the dataset with ID {os.environ["APIFY_DEFAULT_DATASET_ID"]}')
4 Replies
Alexey Udovydchenko
Alexey Udovydchenko•3y ago
add for loop or try to convert book to json with i.e. pip jsonpickle, looks like client handles your data as a single object
xenial-black
xenial-blackOP•3y ago
i seem to be getting just one result still. maybe my code isn't processing my input like i want it to. i noticed it always prints the last item. here's my input code
alist = []
book_id = actor_input['book_id'].replace(',', ' ').split()

for id_value in book_id:
if len(id_value) == 10:
alist.append(id_value)
if len(id_value.replace('-', '')) == 13:
try:
if id_value.startswith('979'):
input.remove(id_value)
print('Please only use 978 ISBNs or ASINs.')
continue
elif not id_value.startswith('979'):
alist.append(pyisbn.convert(id_value))
except:
pass
for a in alist:
page = requests.get('https://www.amazon.com/dp/{}/'.format(a), headers=headers)
doc = bs(page.content, 'html.parser')
alist = []
book_id = actor_input['book_id'].replace(',', ' ').split()

for id_value in book_id:
if len(id_value) == 10:
alist.append(id_value)
if len(id_value.replace('-', '')) == 13:
try:
if id_value.startswith('979'):
input.remove(id_value)
print('Please only use 978 ISBNs or ASINs.')
continue
elif not id_value.startswith('979'):
alist.append(pyisbn.convert(id_value))
except:
pass
for a in alist:
page = requests.get('https://www.amazon.com/dp/{}/'.format(a), headers=headers)
doc = bs(page.content, 'html.parser')
wise-white
wise-white•3y ago
You need to pass a list into push_items. So just print what it is before getting pushed
xenial-black
xenial-blackOP•3y ago
i figured it out. i needed to indent the body of my code 😅 thank you for your help

Did you find this page helpful?