Advertisement
MrPinzon

stock_prices.py

Apr 25th, 2022
918
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. import sys
  2. from PyQt6.QtGui import QPixmap, QFont
  3. from PyQt6.QtWidgets import QMainWindow, QApplication, QLabel, QVBoxLayout, QWidget
  4. from PyQt6 import QtCore
  5.  
  6. import requests #to interact with the internet
  7. from bs4 import BeautifulSoup #this package can understand HTML code
  8.  
  9. #first tell requests to get the webpage info
  10. URL = "https://www.marketwatch.com/investing/stock/gme"
  11. web = requests.get(URL)
  12. soup = BeautifulSoup(web.content, 'html.parser')
  13. #print(soup.prettify())
  14. #print(soup.findAll('h1'))
  15. #print(soup.find('h1', {'class': 'company__name'}).text)
  16. price = soup.find('bg-quote', {'format': '0,0.00', 'field': 'Last'}).text
  17. percent = soup.find('bg-quote', {'format': '0,0.00%', 'session': 'after'}).text
  18. print(price)
  19. print(percent)
  20.  
  21. class MainWindow(QMainWindow):
  22.     def __init__(self):
  23.         super(MainWindow, self).__init__()
  24.         self.title = "Stock prices"
  25.         self.setWindowTitle(self.title)
  26.  
  27.         main_layout = QVBoxLayout()
  28.  
  29.         current_price = QLabel(price)
  30.         current_price.setFont(QFont('Arial', 40))
  31.         current_price.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
  32.         main_layout.addWidget(current_price)
  33.  
  34.         reaction = QLabel(self)
  35.         stonks = QPixmap('stonks.png')
  36.         not_stonks = QPixmap('not_stonks.png')
  37.         if (percent[0] == "-"):
  38.             reaction.setPixmap(not_stonks)
  39.         else:
  40.             reaction.setPixmap(stonks)
  41.  
  42.         main_layout.addWidget(reaction)
  43.  
  44.         widget = QWidget()
  45.         widget.setLayout(main_layout)
  46.         self.setCentralWidget(widget)
  47.  
  48. app = QApplication(sys.argv)
  49. w = MainWindow()
  50. w.show()
  51. app.exec()
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement