Clean up repeated file.writes, if/elses when adding keys to a dict I'm getting familiar with python and I'm still learning it's tricks and idioms.  

Is there an better way to implement print_html() without the multiple calls to html_file.write()?  I only separated the HTML for clarity instead of putting the entire string inside write().

In get_car(), is there a way to avoid using if/else to make the dict key ' ' if listing.find() returns nothing?  

Is there anything that is flat out wrong and should be avoided/improved?

#!/usr/bin/python 

import requests
import bs4
import os

### Declarations

home         = os.environ['HOME']
filename     = home + '/results.html'
results_file = open(filename, 'w')

req = requests.get([url_removed])

soup     = bs4.BeautifulSoup(req.content)
listings = soup.find_all('div', class_='listing listing-findcar listing-dealer     ')
listings.reverse()

### Functions

def print_header(message, html_file):
    html_file.write(message)


def print_html(car, html_file):
    price    = '&lt;h3&gt;' + car['price'].strip() + '&lt;/h3&gt;'
    image    = '&lt;img src="' + car['image'].strip() + '"&gt;'
    year     = '&lt;li&gt;' + car['year'].strip() + ' ' + car['trim'].strip() + '&lt;/li&gt;'
    color    = '&lt;li&gt;' + car['color'].strip() + '&lt;/li&gt;'
    miles    = '&lt;li&gt;' + car['miles'].strip() + '&lt;/li&gt;'
    dealer   = '&lt;li&gt;' + car['dealer'].strip() + '&lt;/li&gt;'
    phone    = '&lt;li&gt;' + car['phone'].strip() + '&lt;/li&gt;'
    distance = '&lt;li&gt;' + car['distance'].strip() + '&lt;/li&gt;'
    link     = '&lt;li&gt;&lt;a href="[url_removed]' + car['link'].strip() + '"&gt;[text_removed]&lt;/a&gt;&lt;/li&gt;'

    html_file.write(price)
    html_file.write(image)
    html_file.write('&lt;ul&gt;')
    html_file.write(year)
    html_file.write(color)
    html_file.write(miles)
    html_file.write(dealer)
    html_file.write(phone)
    html_file.write(distance)
    html_file.write(link)
    html_file.write('&lt;/ul&gt;')


def get_car(listing):
    car = dict()

    # Year
    if listing.find('span', class_='atcui-truncate ymm'):
        car['year'] = listing.find('span', class_='atcui-truncate ymm').get_text()
    else:
        car['year'] = ''

    # Trim
    if listing.find('span', class_='trim'):
        car['trim'] = listing.find('span', class_='trim').get_text()
    else:
        car['trim'] = ''

    # Price
    if listing.find('h4', class_='primary-price'):
        car['price'] = listing.find('h4', class_='primary-price').get_text()
    else:
        car['price'] = ''

    # Mileage
    if listing.find('span', class_='mileage'):
        car['miles'] = listing.find('span', class_='mileage').get_text()
    else:
        car['miles'] = ''

    # Color
    if listing.find('span', class_='color'):
        car['color'] = listing.find('span', class_='color').get_text()
    else:
        car['color'] = ''

    # Distance
    if listing.find('span', class_='distance-cont'):
        car['distance'] = listing.find('span', class_='distance-cont').get_text()
    else:
        car['distance'] = ''

    # Dealer
    if listing.find('span', class_='owner-title atcui-block atcui-bold'):
        car['dealer'] = listing.find('span', class_='owner-title atcui-block atcui-bold').get_text()
    else:
        car['dealer'] = ''

    # Dealer phone
    if listing.find('span', class_='owner-phone'):
        car['phone'] = listing.find('span', class_='owner-phone').get_text()
    else:
        car['phone'] = ''

    # Image
    if listing.find('img', class_='atcui-vehicle-image   '):
        car['image'] = listing.find('img', class_='atcui-vehicle-image').get('src')
    else:
        car['image'] = ''

    # Link
    if listing.find('a', class_='vehicle-title atcui-trigger '):
        car['link'] = listing.find('a', class_='vehicle-title atcui-trigger ').get('href')
    else:
        car['link'] = ''

    return car

### Main

print_header('&lt;h3&gt;' + str(len(listings)) + ' CARS FOUND&lt;/h3&gt;', results_file)

for listing in listings:
    listing_dict = get_car(listing)
    print_html(listing_dict, results_file)
You can make the HTML a multi-line string with formatting templates (see this SO question for discussion on indentation, I've kept it simple):

def print_html(car, html_file):
    html = '''&lt;h3&gt;{0[price]}&lt;/h3&gt;
&lt;img src="{0[image]}"&gt;
&lt;ul&gt;
     &lt;li&gt;{0[year]} {0[trim]}&lt;/li&gt;
     ...
&lt;/ul&gt;'''


Then your writing becomes a single line:

    html_file.write(html.format(car))


Note that this doesn't strip; you should do that when the data go into the dictionary. 



There is a lot of duplication in get_car. Also, you do the find twice, which will slow you down. Try something like:

def get_car(listing):
   car = dict()
   data = {'year': ('span', 'atcui-truncate ymm'),
           ...}

   for key in data:
       element, class_ = data[key]
       result = listing.find(element, class_=class_)
       car[key] = '' if result is None else result.get_text().strip()

   return car 




Finally, you aren't always following the style guide, e.g. lining up operators:

### Declarations

home = os.environ['HOME']
filename = home + '/results.html'
results_file = open(filename, 'w')