Last active
October 14, 2020 00:41
-
-
Save fijiaaron/686e601f428ad6b6e83e2950e38f8eb6 to your computer and use it in GitHub Desktop.
Scrape Craigslist for recent car posts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
requests==2.24.0 | |
beautifulsoup4==4.9.3 | |
# setup: pip install -r requirements.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from time import sleep | |
from bs4 import BeautifulSoup | |
market = 'seattle' | |
max = 600 | |
start = 0 | |
while start < max: | |
url = "https://{market}.craigslist.org/search/cta?s={start}".format(market=market, start=start) | |
# print(url) | |
response = requests.get(url) | |
# print(response.content) | |
soup = BeautifulSoup(response.content, 'html.parser') | |
# print(soup) | |
results = soup.find_all('li', class_='result-row') | |
# print(len(results)) | |
for result in results: | |
date = result.find(class_='result-date') | |
title = result.find(class_='result-title') | |
price = result.find(class_='result-price') | |
hood = result.find(class_='result-hood') | |
tags = result.find(class_='result-tags') | |
print(date.attrs['datetime'], title.text, price.text, end="\n", sep="\t") | |
start = start + 120 | |
sleep(5) | |
## usage: python search_craigslist.py > cars.tsv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment