# default_exp apt
#hide
#export
from fastcore.all import urlread, L, test_eq, call_parse, first
from bs4 import BeautifulSoup as bs
Show apartments for a region
#export
def get_apartments(region:str, #craiglist region like `portland`
):
"return listing of apartments from craigslist"
try:
_pg = bs(urlread(f'https://{region}.craigslist.org/search/apa'), features='lxml')
except:
raise Exception(f"could not pull website for {region}")
return L(_pg.find_all('li', {'class': 'result-row'})).map(lambda x: x.a['href'])
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:
_results = get_apartments('portland')
#hide
test_eq(len(_results), 120)
#export
@call_parse
def show_apts(region:str, #craiglist region like `portland`
n:int, #show n listings
):
"Show links to apartments on craigslist"
print(f"Showing {n} apartments in {region}\n{'='*50}")
for a in get_apartments(region)[:n]:
print(a)