{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# default_exp apt" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#hide\n", "#export\n", "from fastcore.all import urlread, L, test_eq, call_parse, first\n", "from bs4 import BeautifulSoup as bs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Get Apartments\n", "\n", "> Show apartments for a region" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def get_apartments(region:str, #craiglist region like `portland`\n", " ):\n", " \"return listing of apartments from craigslist\"\n", " try:\n", " _pg = bs(urlread(f'https://{region}.craigslist.org/search/apa'), features='lxml')\n", " except:\n", " raise Exception(f\"could not pull website for {region}\")\n", " return L(_pg.find_all('li', {'class': 'result-row'})).map(lambda x: x.a['href'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can use `get apartments` to show links for the most recent listings for a specific `region`. For example, to get the most recent listings for portand, you can run:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "_results = get_apartments('portland')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#hide\n", "test_eq(len(_results), 120)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "\n", "@call_parse\n", "def show_apts(region:str, #craiglist region like `portland`\n", " n:int, #show n listings\n", " ):\n", " \"Show links to apartments on craigslist\"\n", " print(f\"Showing {n} apartments in {region}\\n{'='*50}\")\n", " for a in get_apartments(region)[:n]:\n", " print(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 4 }