Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions your-code/.ipynb_checkpoints/Untitled-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}
6 changes: 6 additions & 0 deletions your-code/.ipynb_checkpoints/Untitled1-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}
219 changes: 219 additions & 0 deletions your-code/.ipynb_checkpoints/liga_mx_data-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
" _ _ ___ _____ __ ______ _ \n",
"| | (_) | \\/ |\\ \\ / / | _ \\ | | \n",
"| | _ __ _ __ _| . . | \\ V / | | | |__ _| |_ __ _ \n",
"| | | |/ _` |/ _` | |\\/| | / \\ | | | / _` | __/ _` |\n",
"| |___| | (_| | (_| | | | |/ /^\\ \\ | |/ / (_| | || (_| |\n",
"\\_____/_|\\__, |\\__,_\\_| |_/\\/ \\/ |___/ \\__,_|\\__\\__,_|\n",
" __/ | \n",
" |___/ \n",
" \n"
]
}
],
"source": [
"import base64\n",
"import pandas as pd\n",
"import requests as r\n",
"import ast\n",
"\n",
"print(\"\"\"\n",
" _ _ ___ _____ __ ______ _ \n",
"| | (_) | \\/ |\\ \\ / / | _ \\ | | \n",
"| | _ __ _ __ _| . . | \\ V / | | | |__ _| |_ __ _ \n",
"| | | |/ _` |/ _` | |\\/| | / \\ | | | / _` | __/ _` |\n",
"| |___| | (_| | (_| | | | |/ /^\\ \\ | |/ / (_| | || (_| |\n",
"\\_____/_|\\__, |\\__,_\\_| |_/\\/ \\/ |___/ \\__,_|\\__\\__,_|\n",
" __/ | \n",
" |___/ \n",
" \"\"\")\n",
"\n",
"\n",
"url = \"https://ligamx.net/cancha/estadisticahistorica/1/\"\n",
"base64_string = b'eyJpZERpdmlzaW9uIjoiMSIsImlkVGVtcG9yYWRhIjoiNzAiLCAiaWRUb3JuZW8iOiIxIn0='\n"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [],
"source": [
"header = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36'} \n",
"\n",
"decoded = base64.decodebytes(base64_string)\n",
"dec = decoded.decode('UTF-8').replace(\"'\", '\"')\n",
"\n",
"d = ast.literal_eval(dec)\n",
"\n",
"n = int(d['idTemporada']) - 1\n",
"\n",
"l = []"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [],
"source": [
"for i in range(11):\n",
" for x in range(2,0,-1):\n",
" l.append(str({'idDivision': '1', 'idTemporada': str(n), 'idTorneo': str(x)}).replace(' ', '').replace(\"idTorneo\", \" idTorneo\").replace(\"'\", '\"').replace('\",\" ', '\", \"'))\n",
" n-=1\n",
" \n",
"encodedStrings = [str(base64.b64encode(bytes(i, 'utf-8')),'utf-8') for i in l]\n",
"urls = [(url + i) for i in encodedStrings]\n",
"\n",
"dfs_list = []"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [],
"source": [
"def create_dfs(url):\n",
" cont = r.get(url, headers = header).content\n",
" t = pd.read_html(cont, attrs = {'class': 'default tbl_grals'})\n",
" t[0].columns = t[0].columns.droplevel(level=0)\n",
" df = t[0]\n",
" columns=[('General','POS'),('General','Club'),('TOTAL','JJ'),('TOTAL','JG'), ('TOTAL','JE'),('TOTAL','JP'),('TOTAL','GF'),('TOTAL','GC'),('TOTAL','Dif'),('TOTAL','PTS'),\n",
" ('LOCAL','JJ'),('LOCAL','JG'),('LOCAL','JE'),('LOCAL','JP'),('LOCAL','GF'),('LOCAL','GC'),('LOCAL','Dif'),('LOCAL','PTS'),\n",
" ('VISITANTE','JJ'),('VISITANTE','JG'),('VISITANTE','JE'),('VISITANTE','JP'),('VISITANTE','GF'),('VISITANTE','GC'),('VISITANTE','Dif'),('VISITANTE','PTS')]\n",
" df.columns=pd.MultiIndex.from_tuples(columns)\n",
" dfs_list.append(df)\n",
"\n",
"temporada = 2018\n",
"torneo = 2\n",
"\n",
"#URL inicial insertada en input, generar DF inicial\n",
"create_dfs(url + str(base64_string))"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[+] Temporada:2018, Torneo:2 --> Appended: ok!\n",
"[+] Temporada:2018, Torneo:1 --> Appended: ok!\n",
"[+] Temporada:2017, Torneo:2 --> Appended: ok!\n",
"[+] Temporada:2017, Torneo:1 --> Appended: ok!\n",
"[+] Temporada:2016, Torneo:2 --> Appended: ok!\n",
"[+] Temporada:2016, Torneo:1 --> Appended: ok!\n",
"[+] Temporada:2015, Torneo:2 --> Appended: ok!\n",
"[+] Temporada:2015, Torneo:1 --> Appended: ok!\n",
"[+] Temporada:2014, Torneo:2 --> Appended: ok!\n",
"[+] Temporada:2014, Torneo:1 --> Appended: ok!\n",
"[+] Temporada:2013, Torneo:2 --> Appended: ok!\n",
"[+] Temporada:2013, Torneo:1 --> Appended: ok!\n",
"[+] Temporada:2012, Torneo:2 --> Appended: ok!\n",
"[+] Temporada:2012, Torneo:1 --> Appended: ok!\n",
"No tables found\n"
]
}
],
"source": [
"try:\n",
" for url in urls:\n",
" create_dfs(url)\n",
" print(f'[+] Temporada:{temporada}, Torneo:{torneo} --> Appended: ok!')\n",
" torneo-=1\n",
" if torneo == 0: \n",
" torneo = 2\n",
" temporada -= 1\n",
"except ValueError as e:\n",
" print(e)\n"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[+] Excel Generado!\n"
]
}
],
"source": [
"with pd.ExcelWriter('Estadisticos_LigaMX.xlsx') as writer:\n",
" dfs_list[0].to_excel(writer, sheet_name=f'Temp 2019, Torneo 1')\n",
" for df in dfs_list:\n",
" df.to_excel(writer, sheet_name=f'Temp {temporada}, Torneo {torneo}')\n",
" torneo-=1\n",
" if torneo == 0: \n",
" torneo = 2\n",
" temporada -= 1\n",
"print(\"[+] Excel Generado!\")"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['news_test.ipynb',\n",
" 'Estadisticos_LigaMX.xlsx',\n",
" 'Untitled.ipynb',\n",
" '.ipynb_checkpoints',\n",
" 'Untitled1.ipynb']"
]
},
"execution_count": 86,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import os\n",
"\n",
"os.listdir()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading