Home
Software Programming Learning
EDA & IP & IC
Goodies
News
Contact Us
Article
Contents Of Article
Previous Article
SNO:
Writer:
Next Article
Title:
import time import re import requests from bs4 import BeautifulSoup def printALine(): print("===================================================") """ Examples of myList.txt and the related stockList myList.txt 2330, 1000 0050, 5000 stockList = {"2330":1000,"0050":5000} """ stockList = {} with open("myList.txt","r") as f: for item in f.readlines(): i = item.strip().split(",") stockList[i[0]]=int(i[1]) while True: earning = 0 printALine() print(f"{time.asctime(time.localtime(time.time()))}") print("STOCK# : TODAY | YESTERDAY | CHANGE") for myStock in stockList: page = requests.get("https://tw.stock.yahoo.com/q/q?s="+myStock) bs = BeautifulSoup(page.text,"lxml") targetTable = str(bs.find_all("table")[2]) targetNumber = re.findall(r"[0-9]+.[0-9]+", targetTable) try: todayPrice = float(targetNumber[0]) yesterdayPrice = float(targetNumber[4]) changePrice = todayPrice - yesterdayPrice print(f"{myStock:8s} : {todayPrice:9.2f} | {yesterdayPrice:9.2f} | {changePrice:6.2f}") earning += stockList[myStock] * changePrice except: print(f"Error: Could not find {myStock}-related information.") print(f"Total Earning Today : {int(earning):,d}") printALine() page = requests.get("https://www.goldlegend.com/") bs = BeautifulSoup(page.text,"lxml") targetH3 = bs.select("h3")[0] print(f"Gold price : USD {targetH3.text} / ounce") page = requests.get("https://www.gck99.com.tw/gold.php") bs = BeautifulSoup(page.text,"lxml") targetTd = bs.select("td") print(f"GCK99 gold ingot purchase price : NTD {int(targetTd[14].text)*10+2900:,d} / tael") printALine() time.sleep(60)
Previous Article
Next Article