CA
Crawlee & Apify•3y ago
conscious-sapphire

Actor runs endlessly and only stops when aborted

import os
import re

from apify_client import ApifyClient
from bs4 import BeautifulSoup
import requests

asin = input("Enter ASIN: ")
page = requests.get(f"https://www.amazon.com/dp/%7Basin%7D%22).text
doc = BeautifulSoup(page, 'html.parser')

book = []

title_find = doc.select("[id=centerCol] h1")
title = str(title_find).split(">")[2].split("<")[0]
price_find = doc.select("[id=tmmSwatches] ul li span:nth-of-type(2)")
price = str(price_find).split("span")[2].split(">")[-1].split("<")[0]
pub_find = doc.select("[id=detailBullets_feature_div] ul li span:nth-of-type(2)")
pub = str(pub_find).split("<span")[2].split(">")[1].split("<")[0]
image_find = doc.select("[id=ebooks-img-canvas] img")[1]
image = str(image_find).split(""")[-4]
book = ["Title: " + title, "Price: " + price, "Publisher: " + pub, "Image: " + image]
for r in book:
print(r)

Initialize the main ApifyClient instance
client = ApifyClient(os.environ['APIFY_TOKEN'], api_url=os.environ['APIFY_API_BASE_URL'])

Get the resource subclient for working with the default dataset of the actor run
default_dataset_client = client.dataset(os.environ['APIFY_DEFAULT_DATASET_ID'])

Finally, push all the results into the dataset
default_dataset_client.push_items(book)

print(f'Results have been saved to the dataset with ID {os.environ["APIFY_DEFAULT_DATASET_ID"]}')
import os
import re

from apify_client import ApifyClient
from bs4 import BeautifulSoup
import requests

asin = input("Enter ASIN: ")
page = requests.get(f"https://www.amazon.com/dp/%7Basin%7D%22).text
doc = BeautifulSoup(page, 'html.parser')

book = []

title_find = doc.select("[id=centerCol] h1")
title = str(title_find).split(">")[2].split("<")[0]
price_find = doc.select("[id=tmmSwatches] ul li span:nth-of-type(2)")
price = str(price_find).split("span")[2].split(">")[-1].split("<")[0]
pub_find = doc.select("[id=detailBullets_feature_div] ul li span:nth-of-type(2)")
pub = str(pub_find).split("<span")[2].split(">")[1].split("<")[0]
image_find = doc.select("[id=ebooks-img-canvas] img")[1]
image = str(image_find).split(""")[-4]
book = ["Title: " + title, "Price: " + price, "Publisher: " + pub, "Image: " + image]
for r in book:
print(r)

Initialize the main ApifyClient instance
client = ApifyClient(os.environ['APIFY_TOKEN'], api_url=os.environ['APIFY_API_BASE_URL'])

Get the resource subclient for working with the default dataset of the actor run
default_dataset_client = client.dataset(os.environ['APIFY_DEFAULT_DATASET_ID'])

Finally, push all the results into the dataset
default_dataset_client.push_items(book)

print(f'Results have been saved to the dataset with ID {os.environ["APIFY_DEFAULT_DATASET_ID"]}')
I'm not sure why it's not finishing. It runs fine when I run it as a Python program. I've tried using the BBC weather actor tutorial as a guide but I'm stumped. What am I doing wrong here? Any help or point in the right direction would be appreciated. Thank you.
4 Replies
Alexey Udovydchenko
Alexey Udovydchenko•3y ago
server endlessly waits for keyboard input asin = input("Enter ASIN: ") you need to parse ACTOR INPUT 😉
from apify_client import ApifyClient
client = ApifyClient(os.environ['APIFY_TOKEN'], api_url=os.environ['APIFY_API_BASE_URL'])
default_kv_store_client = client.key_value_store(os.environ['APIFY_DEFAULT_KEY_VALUE_STORE_ID'])
actor_input = default_kv_store_client.get_record(os.environ['APIFY_INPUT_KEY'])['value']
URLS = actor_input['startUrls']
from apify_client import ApifyClient
client = ApifyClient(os.environ['APIFY_TOKEN'], api_url=os.environ['APIFY_API_BASE_URL'])
default_kv_store_client = client.key_value_store(os.environ['APIFY_DEFAULT_KEY_VALUE_STORE_ID'])
actor_input = default_kv_store_client.get_record(os.environ['APIFY_INPUT_KEY'])['value']
URLS = actor_input['startUrls']
conscious-sapphire
conscious-sapphireOP•3y ago
I thought the input tab would be what the input was taken from. I didn't know I needed this code when creating inside apify, The documentation is very confusing 😅 I appreciate the help will i still have
asin = input("Enter ASIN: ")
asin = input("Enter ASIN: ")
in my code or do i need to remove it. to me it looks like URLS would hold the input, correct?
Alexey Udovydchenko
Alexey Udovydchenko•3y ago
Nope, python input must be removed because its waiting for keyboard input at the server and therefore ends up with endless waiting. All values must be parsed from actor input.
conscious-sapphire
conscious-sapphireOP•3y ago
Thank you

Did you find this page helpful?