{ "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
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 }