import os import re import subprocess import MySQLdb as mdb from MySQLdb import escape_string from bs4 import BeautifulSoup, SoupStrainer import base64 from PIL import Image # pip3 install -U Pillow from io import BytesIO # install google chrome in ubuntu: https://tecadmin.net/setup-selenium-chromedriver-on-ubuntu/ def getEntryToMysql(): a = datetime.datetime.now() tList = list() """ myWordCloudJson = str(myWordCloudJson) p_simpletext = remove_control_chars(p_simpletext) MainKeyword = re_pattern.sub(u'\uFFFD', MainKeyword) p_simpletext = p_simpletext.encode('unicode_escape').decode('unicode_escape') p_timestamp = time.time() """ db = mdb.connect(host="localhost",user="root", passwd="###########99", db="SAMYSTOCKS", use_unicode=True, charset="utf8mb4") cursor = db.cursor() cursor.execute("SET NAMES utf8mb4"); cursor.execute("SET CHARACTER SET utf8mb4"); # Execute the SQL command sql = "INSERT INTO samystocks_webpage (p_company_name, p_company_description,p_investor_relations,p_company_link,p_symbol,p_isin,p_dividend_payer,p_dividend_history,p_trafficlight,p_smileytype,p_qualityscore_details,p_qualityscore,p_lastmodified,p_kurs) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" p_lastmodified = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') try: cursor.execute(sql, ('SELECT p_symbol FROM `samystocks_webpage`')) db.commit() # disconnect from server cursor.close() except Exception as e: print("Error %d: %s" % (e.args[0],e.args[1])) exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] print(exc_type, fname, exc_tb.tb_lineno) for row in cursor.fetchall(): tList.append(str(row)) db.close() b = datetime.datetime.now() delta = b - a print("DBify do getEntryToMysql(): Processing finished after:", delta) return tList p_symbol = "PG" #u_d= "google-chrome --headless --disable-gpu --dump-dom --no-sandbox https://www.samystocks.com/intern/charts.php?a="+p_symbol+" > /home/samystocks/data_installer/d.txt" u_c= "google-chrome --headless --disable-gpu --window-size=1920,1080 --dump-dom --no-sandbox https://www.samystocks.com/intern/dividend.php?a="+p_symbol+" > /home/samystocks/data_installer/d.txt" #os.system(u_d) os.system(u_c) with open('/home/samystocks/data_installer/d.txt', 'r', encoding='utf-8') as fp: dividend=fp.read() fp.close() size = 200, 45 soup = BeautifulSoup(dividend,features="lxml") images = soup.findAll('img') for image in images: #print (image.get('id')) base_content= image['src'] v=image.get('id') if v.find("chartImage") != -1: if base_content.find("data:image/png;base64") != -1: base_content = base_content.replace('data:image/png;base64,','') im = Image.open(BytesIO(base64.b64decode(base_content))) fn_d='/home/www/wwwsamystocks/images/chart/'+p_symbol.lower()+'_chart_org.png' fn_f='/home/www/wwwsamystocks/images/chart/'+p_symbol.lower()+'_chart.png' fn_fs='/home/www/wwwsamystocks/images/chart/'+p_symbol.lower()+'_small_chart.png' fn_fst='/home/www/wwwsamystocks/images/chart/'+p_symbol.lower()+'_small_chart_org.png' im.save(fn_d, 'PNG') im2 = Image.open(fn_d) im_resized = im2.resize(size, Image.ANTIALIAS) im_resized.save(fn_fst, "PNG") os.system("pngcrush -brute -force -l 9 -reduce "+fn_d+" "+fn_f) os.system("pngcrush -brute -force -l 9 -reduce "+fn_fst+" "+fn_fs) if v.find("dividendImage") != -1: if base_content.find("data:image/png;base64") != -1: base_content = base_content.replace('data:image/png;base64,','') im = Image.open(BytesIO(base64.b64decode(base_content))) fn_d='/home/www/wwwsamystocks/images/chart/'+p_symbol.lower()+'_dividend_org.png' fn_f='/home/www/wwwsamystocks/images/chart/'+p_symbol.lower()+'_dividend.png' fn_fs='/home/www/wwwsamystocks/images/chart/'+p_symbol.lower()+'_small_dividend.png' fn_fst='/home/www/wwwsamystocks/images/chart/'+p_symbol.lower()+'_small_dividend_org.png' im.save(fn_d, 'PNG') im2 = Image.open(fn_d) im_resized = im2.resize(size, Image.ANTIALIAS) im_resized.save(fn_fst, "PNG") os.system("pngcrush -brute -force -l 9 -reduce "+fn_d+" "+fn_f) os.system("pngcrush -brute -force -l 9 -reduce "+fn_fst+" "+fn_fs) #todo: pngcrush -force auf die beiden pngs machen mit os.system("") os.unlink("/home/samystocks/data_installer/d.txt") #os.unlink("/home/samystocks/data_installer/c.txt") exit(1)