This is my scrap.py code
from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as uReq
website = \"https://houston.craigslist.org/search/cta\
This is because some cars just have no price, e.g. this one. You can put price to unknown
if there was no price:
price_container = container.findAll('span', {'class':'result-price'})
if len(price_container) > 0:
price = price_container[0].text
else:
price = 'unknown'
Or you could just skip the ones without price so they'll not get written to the file:
price_container = container.findAll('span', {'class':'result-price'})
if len(price_container) == 0:
continue
price = price_container[0].text
How can I sort it by price?
results = []
for container in result_html:
carname = container.a.text
price_container = container.findAll('span', {'class':'result-price'})
if len(price_container) == 0:
continue
price = price_container[0].text.strip('$')
results.append((int(price), carname))
for price, carname in sorted(results):
f.write("{}, {}\n".format(carname, price))
f.close()