import requests
import os
from bs4 import BeautifulSoup
import sys


def search_CL(bedrooms=None, minAsk=None, maxAsk=None, query=None):
    url = "http://seattle.craigslist.org/search/apa"
    params = {}
    for k, v in locals().items():
        if v is not None:
            params[k] = v
    if not params:
        raise ValueError("No keywords given")
    else:
        response = requests.get(url, params=params)
    if response.ok:
        return response.content, response.encoding
    else:
        response.raise_for_status()

response, encoding = search_CL()
with open('apartments.html', 'w') as outfile:
    outfile.write(response)


def read_search_results(results='apartments.html'):
    with open(os.getcwd() + '/' + results, 'r') as source:
        return source.read(), 'utf-8'


def parse_source(body, encoding='utf-8'):
    return BeautifulSoup(body, from_encoding=encoding)


def extract_listings(parsed_html):
    listings = parsed_html.find_all('p', class_="row")
    data = []
    for listing in listings:
        link = listing.find('span', class_='pl').find('a')
        this_listing = {
            'link': link.attrs['href'],
            'description': link.string.strip()
        }
        data.append(this_listing)
    return data


if __name__ == "__main__":
    import pprint
    if len(sys.argv) > 1 and sys.argv[1] == 'test':
        body, encoding = read_search_results()
    else:
        body, encoding = search_CL()
    parsed = parse_source(body, encoding)
    listings = extract_listings(parsed)
    print "Number of listings: {}".format(len(listings))
    pprint.pprint(listings[0])
@@ -1081,16 +1081,177 @@
 nd('a')%0A
+        price = listing.find('span', class_='price')%0A        # price_span = listing.find('span', class_='price')%0A        size = price.next_sibling.strip('%5Cn-/')%0A
         
@@ -1353,16 +1353,85 @@
 .strip()
+,%0A            'price': price.string.strip(),%0A            'size': size
 %0A       
@@ -1674,16 +1674,51 @@
 arch_CL(
+minAsk=500, maxAsk=1000, bedrooms=2
 )%0A    pa
@@ -1880,8 +1880,9 @@
 ings%5B0%5D)
+%0A

Update default values in __name__==__main__ to call search_CL with default values that exist so that price is never None