File size: 2,559 Bytes
8405bf7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"from bs4 import BeautifulSoup\n",
"import re"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def parse(url):\n",
" response = requests.get(url)\n",
" response.raise_for_status()\n",
"\n",
" soup = BeautifulSoup(response.text, \"html.parser\")\n",
"\n",
" pattern = re.compile(r\"—\\d+→\")\n",
"\n",
" cuerpo_div = soup.find(\"div\", id=\"cuerpo\")\n",
"\n",
" text_list = []\n",
" if cuerpo_div:\n",
" for p in cuerpo_div.find_all(\"p\"): # Iterate through all <p> elements\n",
" full_text = \" \".join(p.stripped_strings).replace(\"\\n\", \" \")\n",
" cleaned_text = pattern.sub(\"\", full_text)\n",
" if cleaned_text:\n",
" text_list.append(cleaned_text)\n",
"\n",
" return text_list"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"t1 = parse(\n",
" \"https://www.cervantesvirtual.com/obra-visor/manuela-novela-de-costumbres-colombianas-tomo-primero--0/html/ff1e97e4-82b1-11df-acc7-002185ce6064_2.html\"\n",
")\n",
"t2 = parse(\n",
" \"https://www.cervantesvirtual.com/obra-visor/manuela-novela-de-costumbres-colombianas-tomo-segundo--0/html/ff1ea73e-82b1-11df-acc7-002185ce6064_7.html\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"texts = t1[:-2] + t2[:-2]\n",
"volume = [1] * len(t1[:-2]) + [2] * len(t2[:-2])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"df = pd.DataFrame({\"volume\": volume, \"text\": texts})\n",
"df.to_csv(\"manuela.tsv\", sep='\\t', index=False)\n",
"df.to_parquet(\"manuela.parquet\", index=False)"
]
}
],
"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.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|