Skip to content

Instantly share code, notes, and snippets.

View Ze1598's full-sized avatar

José Fernando Costa Ze1598

  • Porto, Portugal
View GitHub Profile
@Ze1598
Ze1598 / 1_download_data.py
Last active April 26, 2022 22:40
SP500 Forecast sourcing data
#from pandas_datareader import data as web
def download_data(tickers, start_date, end_date):
for ticker in tickers:
try:
print(f'Downloading {ticker}')
# Download data from Yahoo
temp_df = web.DataReader(ticker, 'yahoo', start_date, end_date)
# Create a column with only the ticker symbol
temp_df["Ticker"] = ticker
@Ze1598
Ze1598 / gems.py
Last active April 25, 2022 15:43
Falling gems animation with PyGame
import pygame
from random import randint, choice
# RGB colours
BLACK = (0, 0,0)
WHITE = (255, 255, 255)
GREEN = (44, 205, 23)
RED = (216, 10, 0)
BLUE = (27, 64, 255)
PURPLE = (121, 0, 234)
@Ze1598
Ze1598 / append_ws_v2.py
Created November 28, 2021 16:39
Add a worksheet to an Excel workbook (Version 2)
import pandas as pd
import openpyxl as pxl
from openpyxl.utils.dataframe import dataframe_to_rows
# Name of the workbook we'll be using
filename = 'test_wb.xlsx'
# For demo purposes, create a new 1-sheet workbook
# DataFrame to be inserted in the first worksheet
firstMockDF = pd.DataFrame({
@Ze1598
Ze1598 / save_scraped_data_in_df.py
Last active July 28, 2021 23:34
Arknights - scrape operator level up stats
# Imports
# (...)
# selenium set up
# (...)
# scrape dictionary of characters and output as pickle
# (...)
# Running dataframe to collect operator data into
@Ze1598
Ze1598 / scrape_all_character_pages.py
Created July 28, 2021 23:17
Arknights - scrape operator level up stats
# Imports
# (...)
# selenium set up
# (...)
# scrape dictionary of characters and output as pickle
# (...)
# op_dict is the dictionary with all characters and their URLs
@Ze1598
Ze1598 / get_character_list.py
Last active July 28, 2021 22:41
Arknights - scrape operator level up stats
import requests
import pickle
from bs4 import BeautifulSoup
req = requests.get("https://gamepress.gg/arknights/tools/interactive-operator-list")
soup = BeautifulSoup(req.content, "lxml")
# Get all the table cells (<td>) with information about the operators
op_list = soup.find_all("td", class_="operator-cell")
op_dict = {}
@Ze1598
Ze1598 / level_up_char.py
Last active July 28, 2021 19:18
Arknights - scrape operator level up stats
# imports, driver variable set up and loading a character page
# (...)
def get_stats_per_level(num_levels):
for i in range(num_levels):
# Code to read stats (see previous code snippet)
# (...)
# Get the arrow to increase operator level
@Ze1598
Ze1598 / read_level_stats.py
Created July 28, 2021 18:45
Arknights - scrape operator level up stats
# imports, driver variable set up and loading a character page
# (...)
# First thing we need before reading data: decrease the operator level (even if it is already at the lowest level possible)
# Otherwise some stats may be missing
decrease_level_button = driver.find_element_by_class_name("fa-arrow-left")
decrease_level_button.click()
# Read the opreator class
operator_class = driver.find_element_by_class_name("profession-title").text.strip()
@Ze1598
Ze1598 / close_sticky_add.py
Last active July 28, 2021 18:36
Arknights - scrape operator level up stats
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
# Dynamically install a Chrome web driver and instantiate it
driver = webdriver.Chrome(ChromeDriverManager().install())
# Make the driver wait 7 seconds for the page to load
driver.implicitly_wait(7)
# Go the Phantom's character page
driver.get("https://gamepress.gg/arknights/operator/phantom")
# Element to scroll to
@Ze1598
Ze1598 / idx_min_max.py
Created May 6, 2021 11:26
Find column with max/min value for each row in dataframe
import pandas as pd
# Working data
data = [
{
"Symbol": "KLAC",
"Company": "KLA Corporation",
"Sector": "Information Technology",
"Date": "2018-02-02",
"Price": 99.11979699999999,