This Python script is a web scraper designed to extract the real-time stock price of GoldMoney Inc. (TSE:XAU) from Google Finance using PyQt6 and BeautifulSoup. Because Google Finance often displays a cookie consent pop-up that blocks data extraction, the script initializes a headless-like browser instance using QWebEngineView. It embeds a custom, silent web page class to suppress noisy JavaScript console errors in the terminal. Once the page loads, it automatically injects and executes a JavaScript snippet to find and click the "Accept all" or "Acceptă tot" consent buttons. After a short delay to allow the content to refresh, the script captures the updated HTML source, uses BeautifulSoup to locate the specific price elements via robust CSS selectors, prints the cleaned financial data to the terminal, and cleanly terminates the application.
I used artificial intelligence from Google Finance ... REZULTAT SCRAPING (CURAT) ... strange comments:

Let's see:
from PyQt6.QtWidgets import QApplication
from PyQt6.QtWebEngineWidgets import QWebEngineView
from PyQt6.QtWebEngineCore import QWebEnginePage
from PyQt6.QtCore import QUrl, QTimer
from bs4 import BeautifulSoup
import sys
# Clasă custom pentru pagină care ignoră mesajele de consolă
class SilentWebPage(QWebEnginePage):
def javaScriptConsoleMessage(self, level, message, lineID, sourceID):
# Returnăm nimic pentru a nu afișa mesajele de tip "js: Error..." în terminal
pass
class Scraper(QWebEngineView):
def __init__(self, url):
self.app = QApplication.instance() or QApplication(sys.argv)
super().__init__()
# Setăm pagina custom care reduce la tăcere erorile JS
self.setPage(SilentWebPage(self))
self.loadFinished.connect(self._on_load_finished)
self.setUrl(QUrl(url))
self.app.exec()
def _on_load_finished(self, ok):
if ok:
js_code = """
(function() {
let buttons = document.querySelectorAll('button');
for (let i = 0; i < buttons.length; i++) {
let text = buttons[i].innerText;
if (text.includes('Accept all') || text.includes('Acceptă tot')) {
buttons[i].click();
return true;
}
}
return false;
})();
"""
self.page().runJavaScript(js_code, self._after_consent)
else:
self.app.quit()
def _after_consent(self, clicked):
wait_time = 4000 if clicked else 1500
QTimer.singleShot(wait_time, self._extract_html)
def _extract_html(self):
self.page().toHtml(self.parse_finance_data)
def parse_finance_data(self, html):
soup = BeautifulSoup(html, 'html.parser')
try:
# Selectorul robust care a funcționat anterior
main_price = soup.find("div", {"class": "fxKbKc"})
if not main_price:
price_elements = soup.find_all(attrs={"jsname": "Pdsbrc"})
for el in price_elements:
if "$" in el.text or "CAD" in el.parent.text:
main_price = el
break
print("\n" + "="*40)
print("REZULTAT SCRAPING (CURAT)")
print("="*40)
if main_price:
print(f"Activ: GoldMoney Inc (TSE:XAU)")
print(f"Preț: {main_price.text.strip()}")
else:
print("Eroare: Nu s-a putut găsi prețul.")
print("="*40 + "\n")
except Exception as e:
print(f"Eroare: {e}")
self.app.quit()
if __name__ == "__main__":
url_target = "https://www.google.com/finance/quote/XAU:TSE"
scraper = Scraper(url_target)The output of this source code is:
python test_001.py
========================================
REZULTAT SCRAPING (CURAT)
========================================
Activ: GoldMoney Inc (TSE:XAU)
Preț: $15.81
========================================





