summaryrefslogtreecommitdiffstats
path: root/cl.py
blob: a908a991ce13236004a9c7982278599ffb4c8ce9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import argparse
import sys
import requests
from bs4 import BeautifulSoup


def query_craigslist(baseurl=None, keyword='wrx|sti'):
    if baseurl is None:
        baseurl = 'https://chicago.craigslist.org'
    response = requests.get(baseurl + '/search/pta', params={'query': keyword, 'srchType': 'T'})
    soup = BeautifulSoup(response.content, "html.parser")

    results = soup.find_all('li', {'class': 'result-row'})  # at max 120 results per 1 page
    items = []

    for i in results:
        try:
            title = i.find('a', {'class': 'result-title hdrlnk'}).get_text()
            link = i.find('a', {'class': 'result-title hdrlnk'}).get('href')
            date = i.find('time', {'class': 'result-date'}).get('datetime')
            price = i.find('span', {'class': 'result-price'}).get_text()
            hood = i.find('span', {'class': 'result-hood'}).get_text()
            imgs = i.find('a', {'class': 'result-image gallery'}).get('data-ids').split(',')
            img_1 = 'https://images.craigslist.org/' + imgs[0][2:] + '_300x300.jpg'
            d = {'title': title, 'link': link, 'date': date, 'price': price, 'hood': hood, 'img': img_1}
            items.append(d)
        except AttributeError:
            pass  # ignore empty fields

    return items, baseurl, keyword


def main():
    parser = argparse.ArgumentParser(description="craigslist WRX and STi parts finder", parents=())
    parser.add_argument("-b", "--baseurl", help='baseurl, e.g. https://chicago.craigslist.org')
    parser.add_argument("-k", "--keyword", default='wrx|sti', help='keyword to search')

    args, extra_args = parser.parse_known_args()
    partlist = query_craigslist(args.baseurl, args.keyword)
    print(partlist)


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        pass