Last active
July 11, 2024 05:52
-
-
Save jjsantos01/bc0a9aa66960f7044e7a6206b4f7a35c to your computer and use it in GitHub Desktop.
Conteo del númerototal de artículos existentes en todas las leyes federales mexicanas.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd\n", | |
"import requests\n", | |
"from bs4 import BeautifulSoup as BS\n", | |
"from tika import parser\n", | |
"import re\n", | |
"import os\n", | |
"import urllib\n", | |
"datos = 'D:/datos/leyes/federales'\n", | |
"url_base = 'http://www.diputados.gob.mx/LeyesBiblio'\n", | |
"url = f'{url_base}/index.htm'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 100, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# hace requests\n", | |
"resp = requests.get(url)\n", | |
"soup = BS(resp.content, 'lxml')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 102, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# Selecciona tags con lisks de interés\n", | |
"def selector_tr(tag):\n", | |
" return (tag.name=='tr') & len(tag.select('a[href*=\"pdf/\"]'))>0\n", | |
"trs = soup.select_one('table[style=\"width: 100%; border-collapse: collapse\"]').find_all(selector_tr)\n", | |
"leyes = [{'ley': a.select_one('a[href*=\"ref/\"]').text.strip(), 'file': a.select_one('a[href*=\"pdf/\"]')['href'][4:]} for a in trs]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 107, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# Descarga las leyes\n", | |
"for l in leyes:\n", | |
" file = f'{datos}/{l[\"file\"]}'\n", | |
" if not os.path.exists(file):\n", | |
" try:\n", | |
" urllib.request.urlretrieve(f'{url_base}/pdf/{l[\"file\"]}', file)\n", | |
" except Exception as e:\n", | |
" print(l[\"ley\"], e)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 121, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# Número de artículos por ley\n", | |
"for l in leyes:\n", | |
" file = f'{datos}/{l[\"file\"]}'\n", | |
" raw = parser.from_file(file)\n", | |
" texto = raw['content'].replace('í', 'i').replace('Í', 'i') if raw['content'] is not None else None\n", | |
" l['articulos'] = len(re.findall(r'\\nArticulo \\d{1,4}.?\\.\\-?|\\nArticulo \\d{1,4} \\n|\\n\"Articulo \\d{1,4}.?\\.\\-?', texto, flags=re.I)) if texto is not None else 0" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 125, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"df = pd.DataFrame(leyes)\n", | |
"df.to_csv(f'{datos}/leyes_federales.csv', index=False)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 124, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"36670" | |
] | |
}, | |
"execution_count": 124, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df.articulos.sum()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"hide_input": false, | |
"kernelspec": { | |
"display_name": "Python3", | |
"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.6.4" | |
}, | |
"toc": { | |
"base_numbering": 1, | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"title_cell": "Table of Contents", | |
"title_sidebar": "Contents", | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": true, | |
"toc_window_display": false | |
}, | |
"varInspector": { | |
"cols": { | |
"lenName": 16, | |
"lenType": 16, | |
"lenVar": 40 | |
}, | |
"kernels_config": { | |
"python": { | |
"delete_cmd_postfix": "", | |
"delete_cmd_prefix": "del ", | |
"library": "var_list.py", | |
"varRefreshCmd": "print(var_dic_list())" | |
}, | |
"r": { | |
"delete_cmd_postfix": ") ", | |
"delete_cmd_prefix": "rm(", | |
"library": "var_list.r", | |
"varRefreshCmd": "cat(var_dic_list()) " | |
} | |
}, | |
"types_to_exclude": [ | |
"module", | |
"function", | |
"builtin_function_or_method", | |
"instance", | |
"_Feature" | |
], | |
"window_display": false | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment