Python Craigslist Search Script

Here’s a simple script I wrote for fun. It will search multiple, user-specified areas and compile them in an HTML file.

Download Python Script


It’s simple to use just run it, enter the cities you want to search like so:
rochester,buffalo,newyork

Press Enter

Then enter your query:
bmw m3

Press Enter and then open up the new html file to see your results.

import re
import datetime
import time
import string
import urllib
import urllib2

results = re.compile('

.+

sort by') delay = 100 t = datetime.datetime.now() tyme = time.mktime(t.timetuple()) print "Welcome to CLget!" print "" cityIN = raw_input('City or cities separated by commas and no spaces: ') query = raw_input("Input query: ") s = ',' cities = re.split(s,cityIN) for city in list(set(cities)): url = "http://" + city + ".craigslist.org/search/?areaID=126&subAreaID=&query=" + query.replace(' ', ',') + "&catAbb=sss" #Setup headers to spoof Mozilla dat = None ua = "Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.9.1.4) Gecko/20091007 Firefox/3.5.4" head = {'User-agent': ua} errorcount = 0 #Get page req = urllib2.Request(url, dat, head) try: response = urllib2.urlopen(req) except urllib2.HTTPError, e: if errorcount < 1: errorcount = 1 print "Request failed, retrying in " + delay + " seconds" time.sleep(int(delay)) response = urllib2.urlopen(req) except urllib2.URLError, e: if errorcount < 1: errorcount = 1 print "Request failed, retrying in " + delay + " seconds" time.sleep(int(delay)) response = urllib2.urlopen(req) msg = response.read() errorcount = 0 res = results.findall(msg) res = str(res) res = res.replace('[', '') res = res.replace(']', '') res = res.replace('sort by', '') res = res.replace(chr(39), '') outp = open("results" + str(tyme) + ".html", "a") outp.write(city) outp.write(str(res)) outp.close()
This entry was posted in Python and tagged , , , , , , , . Bookmark the permalink.

One Response to Python Craigslist Search Script

  1. Pingback: Kentucky Derby Results 2010:Calvin Borel Super Saver Horse – Newsopi : Breaking News & Politics (blog) | Top Web News

Leave a Reply

Your email address will not be published. Required fields are marked *