Python Forum
Python script not working after 12 hours
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python script not working after 12 hours
#1
Good Afternoon,

I have a question. I am new to Python and I have a scraping script that is setup in Debian (moving to Raspberry Pi 5) where I scrap the market data from precious metals daily at 3:00pm US CST. The first day it would great.. .then I get scripting errors (Stacktrace) with multiple hexadecimal strings. I was told that it could be the issue with the chromedrive being out of sync. but when I check the versions it matches the version of chrome installed. I am using Selenium for this process.

When I reboot it acts like selenium is not even loaded but when I try to re-install it it states it is there. So honestly I am lost as to how to stablize this script to run daily via my CRON Job. I am sure I am missing something but I am not sure what it could be. I have attached the script since there is nothing in this that is critical or private.

import csv
import os
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager

def fetch_apmex_prices():
    url = "https://www.apmex.com/gold-and-silver-price-charts"
    output_file = "daily_prices.csv"

    # Set up Chrome options (do NOT use headless, xvfb simulates display)
    options = Options()
    options.add_argument("--headless=new")
    options.binary_location = "/usr/bin/google-chrome"
    options.add_argument("--disable-gpu")
    options.add_argument("--no-sandbox")
    options.add_argument("--disable-dev-shm-usage")
    # options.add_argument("--disable-software-rasterizer")

    try:
        # Start Chrome browser
        driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
        driver.get("https://www.google.com")

        # Wait for the elements to load
        wait = WebDriverWait(driver, 15)
        gold_elem = wait.until(EC.presence_of_element_located((
            By.XPATH, "//div[contains(text(), 'Gold')]/following-sibling::div"
        )))
        silver_elem = wait.until(EC.presence_of_element_located((
            By.XPATH, "//div[contains(text(), 'Silver')]/following-sibling::div"
        )))

        # Extract and log data
        gold_price = gold_elem.text.strip()
        silver_price = silver_elem.text.strip()
        timestamp = datetime.now().isoformat()

        print(f"[{timestamp}] ✅ Gold: {gold_price} | Silver: {silver_price}")

        # Write to CSV file (create if it doesn't exist)
        file_exists = os.path.isfile(output_file)
        with open(output_file, "a", newline="") as csvfile:
            writer = csv.writer(csvfile)
            if not file_exists:
                writer.writerow(["Timestamp", "Gold Price", "Silver Price"])
            writer.writerow([timestamp, gold_price, silver_price])

    except Exception as e:
        print(f"❌ An error occurred: {e}")

    finally:
        driver.quit()

if __name__ == "__main__":
    fetch_apmex_prices()
Reply
#2
Please post the _full_ stack trace you get when an error occurs.

Instead of using cron to run your script periodically, you may want to use a systemd Timer Unit instead. This will not directly resolve your problem, but when using systemd Units, you get the logging into systemd's journal "for free", which may help to trace down the error.

Regards, noisefloor
Reply
#3
sure... See attached:

Quote:❌ An error occurred: Message:
Stacktrace:
#0 0x55595a4407fa <unknown>
#1 0x555959ee4e90 <unknown>
#2 0x555959f36870 <unknown>
#3 0x555959f36a61 <unknown>
#4 0x555959f84cb4 <unknown>
#5 0x555959f5c26d <unknown>
#6 0x555959f82135 <unknown>
#7 0x555959f5c013 <unknown>
#8 0x555959f28b3b <unknown>
#9 0x555959f297a1 <unknown>
#10 0x55595a405b9b <unknown>
#11 0x55595a409a8a <unknown>
#12 0x55595a3ed912 <unknown>
#13 0x55595a40a604 <unknown>
#14 0x55595a3d274f <unknown>
#15 0x55595a42e678 <unknown>
#16 0x55595a42e856 <unknown>
#17 0x55595a43f666 <unknown>
#18 0x7fbc4a5641f5 <unknown>
Reply
#4
Ok... this isn't too meanfull indeed. Next step: remove the try..except...finally block completely, let the program crash and show the full stack trace.

Regards, noisefloor
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mechanize and BeautifulSoup read not correct hours vaeVictis 5 5,793 Jan-15-2019, 01:27 PM
Last Post: metulburr
  Web App That Request Data from Another Web Site every 12-hours jomonetta 15 13,180 Sep-26-2018, 04:19 PM
Last Post: snippsat
  My first Python scraping script not working... MattH 11 11,533 Feb-17-2018, 05:55 PM
Last Post: MattH

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020