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 = '<h3>' + car['price'].strip() + '</h3>'
image = '<img src="' + car['image'].strip() + '">'
year = '<li>' + car['year'].strip() + ' ' + car['trim'].strip() + '</li>'
color = '<li>' + car['color'].strip() + '</li>'
miles = '<li>' + car['miles'].strip() + '</li>'
dealer = '<li>' + car['dealer'].strip() + '</li>'
phone = '<li>' + car['phone'].strip() + '</li>'
distance = '<li>' + car['distance'].strip() + '</li>'
link = '<li><a href="[url_removed]' + car['link'].strip() + '">[text_removed]</a></li>'
html_file.write(price)
html_file.write(image)
html_file.write('<ul>')
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('</ul>')
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('<h3>' + str(len(listings)) + ' CARS FOUND</h3>', results_file)
for listing in listings:
listing_dict = get_car(listing)
print_html(listing_dict, results_file)